Add role update call

master
esensar 2018-09-22 19:01:50 +02:00
parent 3e0218b0d8
commit 3b09857dbc
4 changed files with 60 additions and 1 deletions

View File

@ -28,6 +28,22 @@ def create_account(username, email, password):
raise ValueError("Account with given parameters already exists") raise ValueError("Account with given parameters already exists")
def update_account_role(account_id, role_id):
"""
Tries to update account role
:param account_id: Target account id
:param role_id: New role role_id
:type account_id: int
:type role_id: int
:returns: True if role is updated successfully
:rtype: Boolean
"""
acc = Account.get(id=account_id)
acc.role_id = role_id
acc.save()
def create_token(username, password): def create_token(username, password):
""" """
Tries to create token for account with given parameters. Tries to create token for account with given parameters.

View File

@ -38,7 +38,9 @@ class ProtectedResource(Resource):
def add_resources(): def add_resources():
from .resources.account import AccountResource, AccountListResource from .resources.account import (AccountResource,
AccountListResource,
AccountRoleResource)
from .resources.token import TokenResource, ValidateTokenResource from .resources.token import TokenResource, ValidateTokenResource
from .resources.device import (DeviceResource, from .resources.device import (DeviceResource,
DeviceRecordingResource, DeviceRecordingResource,
@ -50,6 +52,7 @@ def add_resources():
api.add_resource(AccountResource, '/v1/accounts/<int:account_id>') api.add_resource(AccountResource, '/v1/accounts/<int:account_id>')
api.add_resource(AccountListResource, '/v1/accounts') api.add_resource(AccountListResource, '/v1/accounts')
api.add_resource(AccountRoleResource, '/v1/accounts/<int:account_id>/role')
api.add_resource(TokenResource, '/v1/token') api.add_resource(TokenResource, '/v1/token')
api.add_resource(ValidateTokenResource, '/v1/token/validate') api.add_resource(ValidateTokenResource, '/v1/token/validate')
api.add_resource(DeviceResource, '/v1/devices/<int:device_id>') api.add_resource(DeviceResource, '/v1/devices/<int:device_id>')

View File

@ -13,6 +13,10 @@ class UserSchema(Schema):
password = fields.Str(required=True, load_only=True) password = fields.Str(required=True, load_only=True)
class RoleUpdateSchema(Schema):
role_id = fields.Integer(required=True, load_only=True, location='json')
class UserWrapperSchema(Schema): class UserWrapperSchema(Schema):
user = fields.Nested(UserSchema, required=True, location='json') user = fields.Nested(UserSchema, required=True, location='json')
@ -25,6 +29,18 @@ class AccountResource(ProtectedResource):
abort(403, message='You can only get your own account', status='error') abort(403, message='You can only get your own account', status='error')
class AccountRoleResource(ProtectedResource):
@use_args(RoleUpdateSchema())
@swag_from('swagger/update_account_role_spec.yaml')
def put(self, args, account_id):
if g.current_account.id == account_id:
abort(403, message='You may not change your own roles',
status='error')
success = accounts.update_account_role(account_id, args['role_id'])
if success:
return '', 204
class AccountListResource(Resource): class AccountListResource(Resource):
@use_args(UserWrapperSchema()) @use_args(UserWrapperSchema())
@swag_from('swagger/create_account_spec.yaml') @swag_from('swagger/create_account_spec.yaml')

View File

@ -0,0 +1,24 @@
Updates an account role
---
tags:
- Account
- Role
parameters:
- in: path
name: account_id
required: true
type: integer
description: Id of the account
- in: body
name: body
required: true
schema:
type: object
required:
- role_id
properties:
role_id:
type: integer
responses:
204:
description: Success