def lookup(route, role_id): ''' Tries to look up the entry in the Rules table whose route matches route. If no rule is found a ValueError is raised. Otherwise the function finds the rule that applies to the given role most directly and returns it. If there is a rule for the given role that is returned. If not the function tries to find a rule for the parent, grandparent, ... If there is not found a matching still the function returns None. ''' Log.debug(__name__, route) items = Rule.all() # Try to match the route directly. This will collect the correct results for # all actions that are explicitly defined (f.i. /mailbox/[^/]+/reply) and for # the create, delete, update and find actions of an object folder. rules = [item for item in items if \ re.match(re.sub("/$", "/?", item.route) + "$", route)] # If there was no rule found assume this is an object view action. # Try to remove the object identifier (f.i. /mailbox/mail123 becomes # /mailbox/) and to match again. if not rules: route = re.sub("(?<=.)/[^/]+$", "/", route) rules = [item for item in items if re.match(item.route + "$", route)] # If there still was no matching rule found raise a ValueError. if not rules: raise ValueError() role = Role.get(role_id) while (role): for item in rules: if Role.get(item.role_id) == role: return item role = Role.get(role.parent_id) return None
def installUsers(): if User.get(1): return Log.debug(Installer.__name__, "Installing users...") items = [ # role email name # password key User(Role.get(2), None, "Gast", None, ""), User(Role.get(5), "*****@*****.**", "Administrator", "pwd", ""), User(Role.get(3), "*****@*****.**", "Benutzer", "pwd", ""), User(Role.get(3), "*****@*****.**", "Test Benutzer", "pwd", ""), ] for item in items: item.create()
def delete(identifier): item = Role.get(int(identifier)) if not item: return mismatch() headline = localize("administration", "roles.delete_headline") text = localize("administration", "roles.delete_description") % (item.name) message = localize("administration", "roles.delete_success") return delete_form(item, headline, text, message, "/roles")
def update(identifier): item = Role.get(int(identifier)) if not item: return mismatch() form = FormRole(obj=item) form.parent_id.choices = [(role.id, role.name) for role in Role.all()] headline = localize("administration", "roles.update_headline") message = localize("administration", "roles.update_success") return update_form(item, form, headline, message, "/roles")
def register_unlock(key): user = User.unique(User.generated == key) if not user: flash(localize("administration", "client.no_account")) user.generated = "" user.role = Role.get(3) user.update() g.session.user_id = user.id g.session.update() flash(localize("administration", "client.unlock_success") % (user.name)) return redirect("/")
def installRoles(): if Role.get(1): return Log.debug(Installer.__name__, "Installing roles...") items = [ # parent name # description Role(None, "Root", "Applies to everyone."), Role(1, "Guest", "Applies to clients who aren't logged in."), Role(1, "User", "Applies to all logged in users."), Role( 3, "Locked", "Applies to clients who haven't activated their account yet."), Role(3, "Administrator", "Has the most extensive permissions."), ] for item in items: item.create()
def confirm(): if User.find(User.email == form.email.data): flash(localize("administration", "client.email_taken")) return redirect(request.path) if User.find(User.name == form.name.data): flash(localize("administration", "client.name_taken")) return redirect(request.path) key = randomkey(24, form.name.data) # TODO: Encrypt here. password = form.password.data user = User(Role.get(4), form.email.data, form.name.data, password, key) user.create() # TODO: Write beautiful mail. link = "http://localhost:5000/register/" + key mailservice.send([form.email.data], "TITLE", link) flash(localize("administration", "client.register_success")) return redirect("/")