def get_group_users(db_session, group): try: group = Group.getByNameOrID(group) except orm_exceptions.NoResultFound: raise HTTPRequestError(404, "No group found with this name or ID") else: return group.users
def getGroupUsers(dbSession, group): try: group = Group.getByNameOrID(group) except sqlalchemy.orm.exc.NoResultFound: raise HTTPRequestError(404, "No group found with this name or ID") else: return group.users
def add_permissions_group(): predef_group_perm = [ { "name": "admin", "permission": [ 'all_all' ] }, { "name": "user", "permission": [ 'all_template', 'all_device', 'all_flows', 'all_history', 'all_metric', 'all_mashup', 'ro_alarms', 'ro_ca', 'wo_sign' ] } ] for group in predef_group_perm: group_id = Group.getByNameOrID(group['name']).id for perm in group['permission']: perm_id = Permission.getByNameOrID(perm).id r = GroupPermission(group_id=group_id, permission_id=perm_id) db.session.add(r) db.session.commit()
def add_user_groups(): predef_user_group = [ { "name": "admin", "groups": ["admin"] }, ] for user in predef_user_group: user_id = User.getByNameOrID(user['name']).id for group_name in user['groups']: r = UserGroup(user_id=user_id, group_id=Group.getByNameOrID(group_name).id) db.session.add(r) db.session.commit()
def updateGroup(dbSession, group, groupData, requester): groupData = {k: groupData[k] for k in groupData if k in Group.fillable} checkGroup(groupData) try: group = Group.getByNameOrID(group) if 'name' in groupData.keys() and group.name != groupData['name']: raise HTTPRequestError(400, "groups name can't be changed") for key, value in groupData.items(): setattr(group, key, value) dbSession.add(group) log().info('group ' + group.name + ' updated by ' + requester['username'], groupData) except sqlalchemy.orm.exc.NoResultFound: raise HTTPRequestError(404, "No group found with this ID")
def delete_group(db_session, group, requester): try: group = Group.getByNameOrID(group) db_session.execute( GroupPermission.__table__.delete( GroupPermission.group_id == group.id)) db_session.execute( UserGroup.__table__.delete(UserGroup.group_id == group.id)) cache.delete_key() log().info( 'group ' + group.name + ' deleted by ' + requester['username'], group.safeDict()) db_session.delete(group) except orm_exceptions.NoResultFound: raise HTTPRequestError(404, "No group found with this ID")
def removeGroupPermission(dbSession, group, permission, requester): try: group = Group.getByNameOrID(group) except sqlalchemy.orm.exc.NoResultFound: raise HTTPRequestError(404, "No group found with this ID or name") try: perm = Permission.getByNameOrID(permission) except sqlalchemy.orm.exc.NoResultFound: raise HTTPRequestError(404, "No permission found with this ID") try: relation = dbSession.query(GroupPermission) \ .filter_by(group_id=group.id, permission_id=perm.id).one() dbSession.delete(relation) cache.deleteKey(action=perm.method, resource=perm.path) log().info('permission ' + perm.name + ' removed from ' ' group ' + group.name + ' by ' + requester['username']) except sqlalchemy.orm.exc.NoResultFound: raise HTTPRequestError(404, "Group does not have this permission")
def removeUserGroup(dbSession, user, group, requester): try: user = User.getByNameOrID(user) except sqlalchemy.orm.exc.NoResultFound: raise HTTPRequestError(404, "No user found with this ID or name") try: group = Group.getByNameOrID(group) except sqlalchemy.orm.exc.NoResultFound: raise HTTPRequestError(404, "No group found with this ID or name") try: relation = dbSession.query(UserGroup) \ .filter_by(user_id=user.id, group_id=group.id).one() dbSession.delete(relation) cache.deleteKey(userid=user.id) log().info('user ' + user.username + ' removed from ' + group.name + ' by ' + requester['username']) except sqlalchemy.orm.exc.NoResultFound: raise HTTPRequestError(404, "User is not a member of the group")
def addUserGroup(dbSession, user, group, requester): try: user = User.getByNameOrID(user) except sqlalchemy.orm.exc.NoResultFound: raise HTTPRequestError(404, "No user found with this ID or name") try: group = Group.getByNameOrID(group) except sqlalchemy.orm.exc.NoResultFound: raise HTTPRequestError(404, "No group found with this ID or name") if dbSession.query(UserGroup).filter_by(user_id=user.id, group_id=group.id).one_or_none(): raise HTTPRequestError(409, "User is already a member of the group") r = UserGroup(user_id=user.id, group_id=group.id) dbSession.add(r) cache.deleteKey(userid=user.id) log().info('user ' + user.username + ' added to group ' + group.name + ' by ' + requester['username'])
def addGroupPermission(dbSession, group, permission, requester): try: group = Group.getByNameOrID(group) except sqlalchemy.orm.exc.NoResultFound: raise HTTPRequestError(404, "No group found with this ID or name") try: perm = Permission.getByNameOrID(permission) except sqlalchemy.orm.exc.NoResultFound: raise HTTPRequestError(404, "No permission found with this ID or name") if dbSession.query(GroupPermission) \ .filter_by(group_id=group.id, permission_id=perm.id).one_or_none(): raise HTTPRequestError(409, "Group already have this permission") r = GroupPermission(group_id=group.id, permission_id=perm.id) dbSession.add(r) cache.deleteKey(action=perm.method, resource=perm.path) log().info('permission ' + perm.name + ' added to group ' + group.name + ' by ' + requester['username'])
def addPermissionsGroup(): predefGroupPerm = [ { "name": "admin", "permission": [ 'all_template', 'all_device', 'all_flows', 'all_history', 'all_metric', 'all_mashup', 'all_user', 'all_pap' ] }, { "name": "user", "permission": [ 'all_template', 'all_device', 'all_flows', 'all_history', 'all_metric', 'all_mashup' ] } ] for g in predefGroupPerm: groupId = Group.getByNameOrID(g['name']).id for perm in g['permission']: permId = Permission.getByNameOrID(perm).id r = GroupPermission(group_id=groupId, permission_id=permId) db.session.add(r) db.session.commit()
def get_group(db_session, group): try: group = Group.getByNameOrID(group) return group except orm_exceptions.NoResultFound: raise HTTPRequestError(404, "No group found with this ID")