mirror of
https://github.com/cwinfo/powerdns-admin.git
synced 2025-07-27 15:54:16 +00:00
upd: improve user api (#878)
This commit is contained in:
@@ -14,13 +14,16 @@ from ..models import (
|
||||
from ..lib import utils, helper
|
||||
from ..lib.schema import (
|
||||
ApiKeySchema, DomainSchema, ApiPlainKeySchema, UserSchema, AccountSchema,
|
||||
UserDetailedSchema,
|
||||
)
|
||||
from ..lib.errors import (
|
||||
StructuredException,
|
||||
DomainNotExists, DomainAlreadyExists, DomainAccessForbidden,
|
||||
RequestIsNotJSON, ApiKeyCreateFail, ApiKeyNotUsable, NotEnoughPrivileges,
|
||||
AccountCreateFail, AccountUpdateFail, AccountDeleteFail,
|
||||
UserCreateFail, UserUpdateFail, UserDeleteFail,
|
||||
AccountCreateDuplicate,
|
||||
UserCreateFail, UserCreateDuplicate, UserUpdateFail, UserDeleteFail,
|
||||
UserUpdateFailEmail,
|
||||
)
|
||||
from ..decorators import (
|
||||
api_basic_auth, api_can_create_domain, is_json, apikey_auth,
|
||||
@@ -33,11 +36,14 @@ import string
|
||||
api_bp = Blueprint('api', __name__, url_prefix='/api/v1')
|
||||
|
||||
apikey_schema = ApiKeySchema(many=True)
|
||||
apikey_single_schema = ApiKeySchema()
|
||||
domain_schema = DomainSchema(many=True)
|
||||
apikey_plain_schema = ApiPlainKeySchema(many=True)
|
||||
apikey_plain_schema = ApiPlainKeySchema()
|
||||
user_schema = UserSchema(many=True)
|
||||
user_single_schema = UserSchema()
|
||||
user_detailed_schema = UserDetailedSchema()
|
||||
account_schema = AccountSchema(many=True)
|
||||
|
||||
account_single_schema = AccountSchema()
|
||||
|
||||
def get_user_domains():
|
||||
domains = db.session.query(Domain) \
|
||||
@@ -360,7 +366,7 @@ def api_generate_apikey():
|
||||
raise ApiKeyCreateFail(message='Api key create failed')
|
||||
|
||||
apikey.plain_key = b64encode(apikey.plain_key.encode('utf-8')).decode('utf-8')
|
||||
return jsonify(apikey_plain_schema.dump([apikey])[0]), 201
|
||||
return jsonify(apikey_plain_schema.dump(apikey)), 201
|
||||
|
||||
|
||||
@api_bp.route('/pdnsadmin/apikeys', defaults={'domain_name': None})
|
||||
@@ -418,7 +424,7 @@ def api_get_apikey(apikey_id):
|
||||
if apikey_id not in [a.id for a in get_user_apikeys()]:
|
||||
raise DomainAccessForbidden()
|
||||
|
||||
return jsonify(apikey_schema.dump([apikey])[0]), 200
|
||||
return jsonify(apikey_single_schema.dump(apikey)), 200
|
||||
|
||||
|
||||
@api_bp.route('/pdnsadmin/apikeys/<int:apikey_id>', methods=['DELETE'])
|
||||
@@ -566,12 +572,12 @@ def api_update_apikey(apikey_id):
|
||||
def api_list_users(username=None):
|
||||
if username is None:
|
||||
user_list = [] or User.query.all()
|
||||
return jsonify(user_schema.dump(user_list)), 200
|
||||
else:
|
||||
user_list = [] or User.query.filter(User.username == username).all()
|
||||
if not user_list:
|
||||
user = User.query.filter(User.username == username).first()
|
||||
if user is None:
|
||||
abort(404)
|
||||
|
||||
return jsonify(user_schema.dump(user_list)), 200
|
||||
return jsonify(user_detailed_schema.dump(user)), 200
|
||||
|
||||
|
||||
@api_bp.route('/pdnsadmin/users', methods=['POST'])
|
||||
@@ -639,12 +645,12 @@ def api_create_user():
|
||||
if not result['status']:
|
||||
current_app.logger.warning('Create user ({}, {}) error: {}'.format(
|
||||
username, email, result['msg']))
|
||||
raise UserCreateFail(message=result['msg'])
|
||||
raise UserCreateDuplicate(message=result['msg'])
|
||||
|
||||
history = History(msg='Created user {0}'.format(user.username),
|
||||
created_by=current_user.username)
|
||||
history.add()
|
||||
return jsonify(user_schema.dump([user])), 201
|
||||
return jsonify(user_single_schema.dump(user)), 201
|
||||
|
||||
|
||||
@api_bp.route('/pdnsadmin/users/<int:user_id>', methods=['PUT'])
|
||||
@@ -708,7 +714,10 @@ def api_update_user(user_id):
|
||||
if not result['status']:
|
||||
current_app.logger.warning('Update user ({}, {}) error: {}'.format(
|
||||
username, email, result['msg']))
|
||||
raise UserCreateFail(message=result['msg'])
|
||||
if result['msg'].startswith('New email'):
|
||||
raise UserUpdateFailEmail(message=result['msg'])
|
||||
else:
|
||||
raise UserCreateFail(message=result['msg'])
|
||||
|
||||
history = History(msg='Updated user {0}'.format(user.username),
|
||||
created_by=current_user.username)
|
||||
@@ -759,25 +768,18 @@ def api_list_accounts(account_name):
|
||||
else:
|
||||
if account_name is None:
|
||||
account_list = [] or Account.query.all()
|
||||
return jsonify(account_schema.dump(account_list)), 200
|
||||
else:
|
||||
account_list = [] or Account.query.filter(
|
||||
Account.name == account_name).all()
|
||||
if not account_list:
|
||||
account = Account.query.filter(
|
||||
Account.name == account_name).first()
|
||||
if account is None:
|
||||
abort(404)
|
||||
if account_name is None:
|
||||
return jsonify(account_schema.dump(account_list)), 200
|
||||
else:
|
||||
return jsonify(account_schema.dump(account_list)[0]), 200
|
||||
return jsonify(account_single_schema.dump(account)), 200
|
||||
|
||||
|
||||
@api_bp.route('/pdnsadmin/accounts', methods=['POST'])
|
||||
@api_basic_auth
|
||||
def api_create_account():
|
||||
account_exists = [] or Account.query.filter(Account.name == account_name).all()
|
||||
if len(account_exists) > 0:
|
||||
msg = "Account name already exists"
|
||||
current_app.logger.debug(msg)
|
||||
raise AccountCreateFail(message=msg)
|
||||
if current_user.role.name not in ['Administrator', 'Operator']:
|
||||
msg = "{} role cannot create accounts".format(current_user.role.name)
|
||||
raise NotEnoughPrivileges(message=msg)
|
||||
@@ -790,6 +792,12 @@ def api_create_account():
|
||||
current_app.logger.debug("Account name missing")
|
||||
abort(400)
|
||||
|
||||
account_exists = [] or Account.query.filter(Account.name == name).all()
|
||||
if len(account_exists) > 0:
|
||||
msg = "Account {} already exists".format(name)
|
||||
current_app.logger.debug(msg)
|
||||
raise AccountCreateDuplicate(message=msg)
|
||||
|
||||
account = Account(name=name,
|
||||
description=description,
|
||||
contact=contact,
|
||||
@@ -806,7 +814,7 @@ def api_create_account():
|
||||
history = History(msg='Create account {0}'.format(account.name),
|
||||
created_by=current_user.username)
|
||||
history.add()
|
||||
return jsonify(account_schema.dump([account])[0]), 201
|
||||
return jsonify(account_single_schema.dump(account)), 201
|
||||
|
||||
|
||||
@api_bp.route('/pdnsadmin/accounts/<int:account_id>', methods=['PUT'])
|
||||
@@ -871,6 +879,7 @@ def api_delete_account(account_id):
|
||||
|
||||
|
||||
@api_bp.route('/pdnsadmin/accounts/users/<int:account_id>', methods=['GET'])
|
||||
@api_bp.route('/pdnsadmin/accounts/<int:account_id>/users', methods=['GET'])
|
||||
@api_basic_auth
|
||||
@api_role_can('list account users')
|
||||
def api_list_account_users(account_id):
|
||||
@@ -885,6 +894,9 @@ def api_list_account_users(account_id):
|
||||
@api_bp.route(
|
||||
'/pdnsadmin/accounts/users/<int:account_id>/<int:user_id>',
|
||||
methods=['PUT'])
|
||||
@api_bp.route(
|
||||
'/pdnsadmin/accounts/<int:account_id>/users/<int:user_id>',
|
||||
methods=['PUT'])
|
||||
@api_basic_auth
|
||||
@api_role_can('add user to account')
|
||||
def api_add_account_user(account_id, user_id):
|
||||
@@ -909,6 +921,9 @@ def api_add_account_user(account_id, user_id):
|
||||
@api_bp.route(
|
||||
'/pdnsadmin/accounts/users/<int:account_id>/<int:user_id>',
|
||||
methods=['DELETE'])
|
||||
@api_bp.route(
|
||||
'/pdnsadmin/accounts/<int:account_id>/users/<int:user_id>',
|
||||
methods=['DELETE'])
|
||||
@api_basic_auth
|
||||
@api_role_can('remove user from account')
|
||||
def api_remove_account_user(account_id, user_id):
|
||||
|
Reference in New Issue
Block a user