def discussion_sysadmin_user(request, test_app, test_session, discussion): """A User fixture with R_SYSADMIN role in a discussion""" from datetime import datetime from assembl.auth import R_SYSADMIN from assembl.models import User from assembl.models.auth import Role, LocalUserRole, UserRole u = User(name=u"Maximilien de Robespierre 3", type="user", last_assembl_login=datetime.utcnow()) test_session.add(u) u.update_agent_status_last_visit(discussion) role = Role.get_role(R_SYSADMIN, test_session) test_session.add(UserRole(user=u, role=role)) test_session.flush() def fin(): print "finalizer discussion_sysadmin_user" test_session.delete(u) test_session.flush() request.addfinalizer(fin) return u
def admin_user(request, test_session): """A User fixture with R_SYSADMIN role""" from assembl.models import User, Username, UserRole, Role u = User(name=u"Mr. Administrator", type="user", verified=True, last_assembl_login=datetime.utcnow()) u.username_p = "mr_admin_user" from assembl.models import EmailAccount account = EmailAccount(email="*****@*****.**", profile=u, verified=True) test_session.add(u) test_session.add(account) r = Role.get_role(R_SYSADMIN, test_session) ur = UserRole(user=u, role=r) test_session.add(ur) test_session.flush() uid = u.id def fin(): print("finalizer admin_user") # I often get expired objects here, and I need to figure out why user = test_session.query(User).get(uid) user_role = user.roles[0] test_session.delete(user_role) account = user.accounts[0] test_session.delete(account) test_session.delete(user.username) test_session.delete(user) test_session.flush() request.addfinalizer(fin) return u
def put_global_roles_for_user(request): user_id = request.matchdict['user_id'] user = User.get_instance(user_id) session = User.default_db if not user: raise HTTPNotFound("User id %d does not exist" % (user_id,)) try: data = json.loads(request.body) except Exception as e: raise HTTPBadRequest("Malformed Json") if not isinstance(data, list): raise HTTPBadRequest("Not a list") if data and frozenset((type(x) for x in data)) != frozenset((str,)): raise HTTPBadRequest("not strings") roles = set(session.query(Role).filter(Role.name.in_(data)).all()) data = set(data) if len(roles) != len(data): raise HTTPBadRequest("Not valid roles: %s" % (repr( data - set((p.name for p in roles))),)) known_gu_roles = session.query(UserRole).join(Role).filter( user=user).all() gur_by_role = {gur.role.name: gur for gur in known_gu_roles} known_roles = set(gur_by_role.keys()) for role in known_roles - roles: session.query.delete(gur_by_role[role]) for role in roles - known_roles: session.add(UserRole(user=user, role=role)) return {"added": list(roles - known_roles), "removed": list(known_roles - roles)}
def participant1_user(request, test_session, discussion): """A User fixture with R_PARTICIPANT global role and with R_PARTICIPANT local role in discussion `discussion`""" from assembl.models import User, UserRole, Role, EmailAccount u = User(name=u"A. Barking Loon", type="user", password="******", verified=True, last_assembl_login=datetime.utcnow()) email = EmailAccount(email="*****@*****.**", profile=u, verified=True) test_session.add(u) r = Role.get_role(R_PARTICIPANT, test_session) ur = UserRole(user=u, role=r) test_session.add(ur) u.subscribe(discussion) test_session.flush() def fin(): print "finalizer participant1_user" test_session.delete(u) test_session.flush() request.addfinalizer(fin) return u
def participant2_user(request, test_session): from assembl.models import User, UserRole, Role u = User(name=u"James T. Expert", type="user") test_session.add(u) r = Role.get_role(R_PARTICIPANT, test_session) ur = UserRole(user=u, role=r) test_session.add(ur) test_session.flush() def fin(): print "finalizer participant2_user" test_session.delete(u) test_session.flush() request.addfinalizer(fin) return u
def admin_user(request, test_session, db_default_data): from assembl.models import User, UserRole, Role u = User(name=u"Mr. Administrator", type="user") test_session.add(u) r = Role.get_role(R_SYSADMIN, test_session) ur = UserRole(user=u, role=r) test_session.add(ur) test_session.flush() def fin(): print "finalizer admin_user" test_session.delete(u) test_session.flush() request.addfinalizer(fin) return u
def participant1_user(request, test_session, discussion): from assembl.models import User, UserRole, Role, EmailAccount u = User(name=u"A. Barking Loon", type="user", password="******", verified=True) email = EmailAccount(email="*****@*****.**", profile=u, verified=True) test_session.add(u) r = Role.get_role(R_PARTICIPANT, test_session) ur = UserRole(user=u, role=r) test_session.add(ur) u.subscribe(discussion) test_session.flush() def fin(): print "finalizer participant1_user" test_session.delete(u) test_session.flush() request.addfinalizer(fin) return u
def participant2_user(request, test_session): """A User fixture with R_PARTICIPANT role""" from assembl.models import User, UserRole, Role u = User(name=u"James T. Expert", type="user", last_idealoom_login=datetime.utcnow()) test_session.add(u) r = Role.get_role(R_PARTICIPANT, test_session) ur = UserRole(user=u, role=r) test_session.add(ur) test_session.flush() def fin(): print("finalizer participant2_user") test_session.delete(u) test_session.flush() request.addfinalizer(fin) return u
def admin_user(request, test_session, db_default_data): """A User fixture with R_SYSADMIN role""" from assembl.models import User, UserRole, Role u = User(name=u"Mr. Administrator", type="user") test_session.add(u) r = Role.get_role(R_SYSADMIN, test_session) ur = UserRole(user=u, role=r) test_session.add(ur) test_session.flush() uid = u.id def fin(): print("finalizer admin_user") # I often get expired objects here, and I need to figure out why user = test_session.query(User).get(uid) user_role = user.roles[0] test_session.delete(user_role) test_session.delete(user) test_session.flush() request.addfinalizer(fin) return u
def moderator_user(request, test_session, discussion): """A User fixture with R_MODERATOR role""" from assembl.models import User, UserRole, Role, EmailAccount u = User(name=u"Jane Doe", type="user", password="******", verified=True, last_assembl_login=datetime.utcnow()) email = EmailAccount(email="*****@*****.**", profile=u, verified=True) test_session.add(u) r = Role.get_role(R_MODERATOR, test_session) ur = UserRole(user=u, role=r) test_session.add(ur) u.subscribe(discussion) test_session.flush() def fin(): print "finalizer moderator_user" test_session.delete(u) test_session.flush() request.addfinalizer(fin) return u
def general_permissions(request): user_id = authenticated_userid(request) assert user_id db = Discussion.default_db roles = db.query(Role).all() roles_by_name = {r.name: r for r in roles} role_names = [r.name for r in roles] role_names.sort() permissions = db.query(Permission).all() permission_names = [p.name for p in permissions] permission_names.sort() user_roles = db.query(UserRole).join(Role, User).all() user_roles_as_set = set((lur.user.id, lur.role.name) for lur in user_roles) user_roles_dict = {(lur.user.id, lur.role.name): lur for lur in user_roles} users = set(lur.user for lur in user_roles) if request.POST: if 'submit_user_roles' in request.POST: user_ids = {u.id for u in users} for role in role_names: if role == Everyone: continue prefix = 'has_' + role + '_' for name in request.POST: if name.startswith(prefix): a_user_id = int(name[len(prefix):]) if a_user_id not in user_ids: users.add(User.get_instance(a_user_id)) user_ids.add(a_user_id) for user in users: has_role_text = 'has_%s_%d' % (role, user.id) if (user.id, role) not in user_roles_as_set and \ has_role_text in request.POST: ur = UserRole(role=roles_by_name[role], user=user) user_roles.append(ur) user_roles_dict[(user.id, role)] = ur user_roles_as_set.add((user.id, role)) db.add(ur) elif (user.id, role) in user_roles_as_set and \ has_role_text not in request.POST: ur = user_roles_dict[(user.id, role)] del user_roles_dict[(user.id, role)] user_roles_as_set.remove((user.id, role)) user_roles.remove(ur) db.delete(ur) elif 'submit_look_for_user' in request.POST: search_string = '%' + request.POST['user_search'] + '%' other_users = db.query(User).outerjoin(Username).filter( AgentProfile.name.ilike(search_string) | Username.username.ilike(search_string) | User.preferred_email.ilike(search_string)).all() users.update(other_users) def has_role(user_id, role): return (user_id, role) in user_roles_as_set users = list(users) users.sort(key=order_by_domain_and_name) context = dict(get_default_context(request), roles=role_names, permissions=permission_names, users=users, has_role=has_role, is_system_role=lambda r: r in SYSTEM_ROLES) return render_to_response('admin/global_permissions.jinja2', context, request=request)