from app.auth import role_required, NO_USER_FOUND_MESSAGE from app import model from app_common.const import BAD_USER_ROLES from app_common.parser import QuarterRequestParser from app.auth.request_parsers import edit_user_parser, admin_crate_user_parser, roles from flask_restx import Namespace, Resource, abort, inputs from flask import jsonify, make_response namespace = Namespace('network_admin', description='API endpoints available for network admins', decorators=[role_required([model.Roles.ADMIN, model.Roles.NETWORK_ADMIN])]) @namespace.route('/ajax/user/') @namespace.response(404, NO_USER_FOUND_MESSAGE) class User(Resource): @namespace.doc(description='Gets the data for a user.') def get(self, user_id): user = model.User.objects(user_id=user_id).first_or_404() return make_response(jsonify(user.to_dict())) @namespace.expect(admin_crate_user_parser) @namespace.doc(description='Crates a new user.') def put(self, user_id): args = admin_crate_user_parser.parse_args() args.user_id = user_id model.User.from_request_args(**dict(args)) @namespace.expect(edit_user_parser) @namespace.doc(description='Update the data for a user.') def post(self, user_id): args = edit_user_parser.parse_args() args.user_id = user_id user = model.User.objects(user_id=args.user_id).first_or_404() # try to update the user for key in dict(args): value = getattr(args, key) if value: setattr(user, key, value) user.save() @namespace.doc(description='Deletes a user.') def delete(self, user_id): user = model.User.objects(user_id=user_id).first_or_404() user.delete() # see if there is an application to delete as well... application = model.Application.objects(user_id=user_id).first() if application: application.delete() get_users_parser = QuarterRequestParser() get_users_parser.add_argument('roles', required=False, type=roles, help='A comma separated list of user roles to return.', default=None) @namespace.route('/users') class GetUsers(Resource): @namespace.expect(get_users_parser) def get(self): args = get_users_parser.parse_args() if args.roles: users = model.User.objects(roles__in=args.roles).all() else: users = model.User.objects() return jsonify([u.to_dict() for u in users]) @namespace.route('/ajax/user//application') class UserApplication(Resource): @namespace.response(404, 'No user was found or no application was found for the given user') def get(self, user_id): user = model.User.objects(user_id=user_id).first_or_404(message='No user found') application = model.Application.objects(applicants__match={'email': user.email}).first_or_404(message='No application found for user.') return make_response(jsonify(user=user.to_dict(), application=application.to_dict())) user_roles_parser = QuarterRequestParser() user_roles_parser.add_argument('roles', required=True, default='', type=roles, help='A comma separated list of roles to assign to/delete from a user') user_roles_parser.add_argument('force_match', required=False, default=False, type=inputs.boolean, help='Weather or not to force a users roles to exactly match what is given.') @namespace.route('/ajax/user//roles') class UserRoles(Resource): @namespace.response(404, 'No user found with the given ID') def get(self, user_id): user = model.User.objects(user_id=user_id).first_or_404(message='No user found') return jsonify(user.roles) @namespace.expect(user_roles_parser) @namespace.response(404, 'No user found with the given ID') def post(self, user_id): args = user_roles_parser.parse_args() user = model.User.objects(user_id=user_id).first_or_404(message='No user found') # if we are supposed to force the user roles to match what is given, clear out the existing roles if args.force_match: user.roles = [] # add the new roles user.roles.extend(args.roles) user.save() @namespace.expect(user_roles_parser) @namespace.response(404, 'No user found with the given ID') def delete(self, user_id): args = user_roles_parser.parse_args() user = model.User.objects(user_id=user_id).first_or_404(message='No user found') user.roles = [r for r in user.roles if r not in args.roles] user.save()