Пример #1
0
    def post(self):
        claims = get_jwt_claims()

        if 'admin' not in claims['authorities']:
            return {'message': 'You dont have permision to perform this operation'}, 401

        data = parser.parse_args()

        if UserModel.find_by_login(data['login']):
            return {'message': 'User {} already exists'.format(data['login'])}

        new_user = UserModel(
            login=data['login'],
            password=UserModel.generate_hash(data['password']),
            firstName=data['firstName'],
            lastName=data['lastName'],
            email=data['email']
        )

        for authority in data['authorities']:
            new_user_authority = AuthorityModel.find_by_authority_name(authority)
            if new_user_authority:
                new_user.authorities.append(new_user_authority)

        try:
            new_user.save_to_db()

            return {'message': 'User {} was create'.format(new_user.login)}
        except:
            return {'message': 'Something went wrong'}, 500
Пример #2
0
    def post(self):
        claims = get_jwt_claims()

        if 'admin' not in claims['roles']:
            return {
                'message': 'You dont have persmision to perform this operation'
            }, 401

        data = parser.parse_args()

        if UserModel.find_by_username(data['username']):
            return {
                'message': 'User {} already exists'.format(data['username'])
            }

        new_user = UserModel(username=data['username'],
                             password=UserModel.generate_hash(
                                 data['password']),
                             firstName=data['firstName'],
                             lastName=data['lastName'])

        for role in data['roles']:
            new_user_role = RoleModel.find_by_role_name(role)
            if new_user_role:
                new_user.roles.append(new_user_role)

        try:
            new_user.save_to_db()
            # access_token = create_access_token(identity=new_user)
            # refresh_token = create_refresh_token(identity=new_user)
            return {
                'message': 'User {} was created'.format(new_user.username)
                # 'access_token': access_token,
                # 'refresh_token': refresh_token
            }
        except:
            return {'message': 'Something went wrong'}, 500