示例#1
0
文件: endpoint.py 项目: l3p-cv/lost
    def patch(self, id):
        args = update_user_parser.parse_args()
        dbm = access.DBMan(LOST_CONFIG)
        identity = get_jwt_identity()
        user = dbm.get_user_by_id(identity)
        if not user.has_role(roles.ADMINISTRATOR):
            dbm.close_session()
            return "You are not authorized.", 401

        requesteduser = dbm.get_user_by_id(id)

        if requesteduser:
            if not requesteduser.is_external:
                requesteduser.email = args.get('email')
                requesteduser.first_name = args.get('first_name')
                requesteduser.last_name = args.get('last_name')

            if roles.ADMINISTRATOR not in args.get('roles'):
                for user_role in dbm.get_user_roles_by_user_id(id):
                    if user_role.role.name == roles.ADMINISTRATOR and requesteduser.user_name != 'admin':
                        dbm.delete(user_role)
                        dbm.commit()

            if args.get('roles'):
                for role_name in args.get('roles'):
                    if role_name == 'Designer':
                        designer_role = dbm.get_role_by_name(roles.DESIGNER)
                        requesteduser.roles.append(designer_role)
                    if role_name == 'Administrator':
                        admin_role = dbm.get_role_by_name(roles.ADMINISTRATOR)
                        requesteduser.roles.append(admin_role)

            for user_group in dbm.get_user_groups_by_user_id(id):
                if user_group.group.is_user_default:
                    continue
                dbm.delete(user_group)
                dbm.commit()
            if args.get('groups'):
                for group_name in args.get('groups'):
                    group = dbm.get_group_by_name(group_name)
                    if group:
                        requesteduser.groups.append(group)
            if args.get('password') and not requesteduser.is_external:
                print(args.get('password'))
                requesteduser.set_password(args.get('password'))

            dbm.save_obj(requesteduser)
            dbm.close_session()
            return 'success', 200
        else:
            dbm.close_session()
            return "User with ID '{}' not found.".format(id), 400
示例#2
0
 def patch(self):
     args = update_user_parser.parse_args()
     dbm = access.DBMan(LOST_CONFIG)
     identity = get_jwt_identity()
     user = dbm.get_user_by_id(identity)
     if user:
         user.email = args.get('email') 
         user.first_name = args.get('first_name')
         user.last_name = args.get('last_name')
         if args.get('password'):
             user.set_password(args.get('password'))
         dbm.save_obj(user)
         dbm.close_session()
         return 'success', 200
     else:
         dbm.close_session()
         return "No user found.", 405