Beispiel #1
0
    def get(self, app_context_id=None):
        if not g.user.is_root_admin:
            return abort(401)
        if not app_context_id:
            app_contexts = AppContext.query.all()
            return {
                'app_contexts':
                [app_context.serialized for app_context in app_contexts],
                'is_successful':
                True
            }

        app_context = AppContext.query.filter_by(id=app_context_id).first()

        if not app_context:
            abort(401)

        return app_context.serialized
Beispiel #2
0
 def delete(self, app_context_id=None):
     if not g.user.is_root_admin:
         return abort(401)
     app_context = AppContext.query.filter_by(id=app_context_id).first()
     if not app_context:
         return {
             'message': 'App context does not exist',
             'is_successful': False
         }
     app_context.delete()
     return {
         'message': 'AppContext deleted successfully.',
         'is_successful': True
     }
Beispiel #3
0
 def delete(self, user_id):
     if not g.user.can.delete.user_id(user_id):
         return abort(401)
     user = User.query.filter_by(id=user_id).first()
     if user and not user.is_deleted:
         user.roles = []
         user.is_deleted = True
         db.session.commit()
         return {
             'message': 'User deleted successfully.',
             'is_successful': True
         }
     else:
         return {
             'message': 'User id: {} is not found'.format(user_id)
         }
Beispiel #4
0
    def post(self, category_id=None):
        data = request.values

        if data.get('id', category_id):
            if not g.user.can.modify.category_id(data.get('id', category_id)):
                return abort(401)
            category_check = Category.query.filter(
                (Category.name == data.get('name', None))
                & (Category.id != data.get('id', None))).first()
            if category_check:
                return {
                    'message': 'Category name already in use under category ID {}' \
                        .format(category_check.id),
                    'is_successful': False
                }
            category = Category.query.filter_by(
                id=data.get('id', category_id)).first()
            if not category:
                return {
                    'message': 'Category does not exist',
                    'is_successful': False
                }
            category.name = data.get('name', category.name)
            db.session.commit()
            return {
                'message': 'Category updated successfully.',
                'is_successful': True
            }

        category_check = Category.query.filter_by(
            name=data.get('name', category_id)).first()
        if category_check:
            return {
                'message': 'Category name already in use under category ID {}' \
                    .format(category_check.id),
                'is_successful': False
            }
        category = Category().create(name=data.get('name', None))

        db.session.commit()

        return {
            'message': 'Category added successfully.',
            'category_id': category.id,
            'is_successful': True
        }
Beispiel #5
0
 def delete(self, scope_id=None):
     if not g.user.can.delete.scope_id(scope_id):
         return abort(401)
     scope = Scope.query.filter_by(id=scope_id).first()
     if not scope:
         return {
             'message': 'Scope does not exist',
             'is_successful': False
         }
     if scope.children or scope.user_scope_mappings:
         return {
             'message': ('Cannot delete scope. Please remove all attached roles and ', 
                         'children scopes first.')
         }
     else:
         scope.delete()
         return {
             'message': 'Scope deleted successfully.'
         }
Beispiel #6
0
    def get(self, category_id=None):

        if not category_id:
            categories = Category.query.all()
            return {
                'categories': [
                    category.serialized for category in list(
                        filter(lambda d: g.user.can.view.category_id(d.id),
                               categories))
                ],
                'is_successful':
                True
            }

        category = Category.query.filter_by(id=category_id).first()
        if g.user.can.view.category_id(category.id):
            return category.serialized
        else:
            return abort(401)
Beispiel #7
0
 def delete(self, category_id=None):
     if not g.user.can.delete.category_id(category_id):
         return abort(401)
     category = Category.query.filter_by(id=category_id).first()
     if not category:
         return {
             'message': 'Category does not exist',
             'is_successful': False
         }
     if category.scopes:
         return {
             'message':
             'Cannot delete category. Please remove all attached scopes first.',
             'is_successful': False
         }
     else:
         category.delete()
         return {
             'message': 'Category deleted successfully.',
             'is_successful': True
         }
Beispiel #8
0
 def delete(self, user_scope_mapping_id=None):
     if not g.user.can.delete.user_scope_mapping_id(user_scope_mapping_id):
         return abort(401)
     user_scope_mapping = UserScopeMapping.query.filter_by(
         id=user_scope_mapping_id).first()
     if not user_scope_mapping:
         return {
             'message': 'User scope mapping does not exist',
             'is_successful': False
         }
     if len(user_scope_mapping.users) > 1 and user_scope_mapping:
         return {
             'message':
             'Cannot delete user_scope_mapping. Remove other users first.',
             'is_successful': False
         }
     else:
         user_scope_mapping.delete()
         return {
             'message': 'Scope deleted successfully.',
             'is_successful': True
         }
Beispiel #9
0
    def get(self, scope_id=None):
        parser.add_argument('treeView', location='args')
        args = parser.parse_args()
        if not scope_id:
            if args.get('treeView', None):
                root_scope = Scope.query.filter_by(parent_scope_id=None).first()
                return {
                    'scopes': root_scope.tree_serialized(g.user),
                    'is_successful': True
                }
            else:
                scopes = Scope.query.all()
                return {
                    'scopes': [scope.serialized for scope in list(filter(lambda d: g.user.can.view.scope_id(d.id), scopes))],
                    'is_successful': True
                }

        scope = Scope.query.filter_by(id=scope_id).first()

        if g.user.can.view.scope_id(scope.id):
            return scope.serialized  
        else:
            return abort(401)
Beispiel #10
0
    def get(self, user_id=None):
        parser.add_argument('returnIdOnly', location='args')
        args = parser.parse_args()
        # list users
        if not user_id:
            users = User.query.filter_by(is_deleted=False).all()
            if args.get('returnIdOnly', None) == 'true':
                return {
                    'users': [user.id for user in 
                                list(filter(lambda d: g.user.can.view.user_id(d.id), users))],
                    'is_successful': True
                }
            return {
                'users': [user.serialized for user in 
                                list(filter(lambda d: g.user.can.view.user_id(d.id), users))],
                'is_successful': True
            }

        user = User.query.filter_by(id=user_id).first()

        if g.user.can.view.user_id(user.id):
            return user.serialized  
        else: 
            return abort(401)
Beispiel #11
0
    def get(self,
            user_scope_mapping_id=None,
            scope_id=None,
            category_id=None,
            user_id=None):
        if scope_id:
            user_scope_mappings = UserScopeMapping.query.filter_by(
                scope_id=scope_id).all()
            user_scope_mappings_list = []
            for mapping in user_scope_mappings:
                for user in mapping.users:
                    if g.user.can.view.user_scope_mapping_id(mapping.id):
                        serialized = mapping.serialized
                        serialized['user_id'] = user.id
                        user_scope_mappings_list.append(serialized)
            if not user_scope_mappings:
                return {
                    'message': 'userScopeMapping not found',
                    'is_successful': False
                }
            return {
                'user_scope_mappings': user_scope_mappings_list,
                'is_successful': True
            }

        if user_id:
            user = User.query.filter_by(id=user_id).first()
            user_scope_mappings = UserScopeMapping.query.join(User.user_scope_mappings) \
                                                                    .filter(User.id == user.id).all()
            if not user:
                return {'message': 'User not found', 'is_successful': False}
            return {
                'user_scope_mappings': [
                    user_scope_mapping.serialized
                    for user_scope_mapping in list(
                        filter(
                            lambda d: g.user.can.view.user_scope_mapping_id(
                                d.id), user_scope_mappings))
                ],
                'is_successful':
                True
            }

        if category_id:
            categories = Category.query.filter_by(id=category_id).all()
            user_scope_mappings = []
            for category in categories:
                for scope in category.scopes:
                    for mappings in scope.user_scope_mappings:
                        user_scope_mappings.append(mappings)
            user_scope_mappings_list = []
            for mapping in user_scope_mappings:
                for user in mapping.users:
                    if g.user.can.view.user_scope_mapping_id(mapping.id):
                        serialized = mapping.serialized
                        serialized['user_id'] = user.id
                        user_scope_mappings_list.append(serialized)
            return {
                'user_scope_mappings': user_scope_mappings_list,
                'is_successful': True
            }

        if not user_scope_mapping_id:
            user_scope_mappings = UserScopeMapping.query.all()
            user_scope_mappings_list = []
            for mapping in user_scope_mappings:
                for user in mapping.users:
                    if g.user.can.view.user_scope_mapping_id(mapping.id):
                        serialized = mapping.serialized
                        serialized['user_id'] = user.id
                        user_scope_mappings_list.append(serialized)
            return {
                'user_scope_mappings': user_scope_mappings_list,
                'is_successful': True
            }

        user_scope_mapping = UserScopeMapping.query.filter_by(
            id=user_scope_mapping_id).first()

        if g.user.can.view.user_scope_mapping_id(user_scope_mapping.id):
            user_scope_mappings_list = []
            for user in user_scope_mapping.users:
                if g.user.can.view.user_scope_mapping_id(
                        user_scope_mapping.id):
                    serialized = user_scope_mapping.serialized
                    serialized['user_id'] = user.id
                    user_scope_mappings_list.append(serialized)
            return {
                'user_scope_mappings': user_scope_mappings_list,
                'is_successful': True
            }
        else:
            return abort(401)
Beispiel #12
0
    def post(self, user_scope_mapping_id=None):
        data = request.values

        user = User.query.filter_by(id=data.get('user_id', None)).first()
        if not user:
            return {
                'message': 'Unable to add/modify user_scope_mapping for user id: {}. User not found' \
                    .format(data.get('user_id', None)),
                'is_successful': False
            }

        if data.get('user_scope_mapping_id', user_scope_mapping_id):
            if not g.user.can.modify.user_scope_mapping_id(
                    data.get('user_scope_mapping_id', user_scope_mapping_id)):
                return abort(401)
            user_scope_mapping = UserScopeMapping.query.filter(
                (UserScopeMapping.id == data.get('user_scope_mapping_id',
                                                 user_scope_mapping_id))
                & (UserScopeMapping.users.any(
                    id=data.get('user_id', None)))).first()
            if not user_scope_mapping:
                return {
                    'message': 'UserScopeMapping not found for user id: {}' \
                        .format(data.get('user_id', None)),
                    'is_successful': False
                }
            if not data.get('role', None) in Config.APPROVED_ROLES:
                return {
                    'message':
                    'Role must be in {}'.format(str(Config.APPROVED_ROLES)),
                    'is_successful':
                    False
                }
            user_scope_mapping.role = data.get('role', user_scope_mapping.role)
            user_scope_mapping.scope_id = data.get('scope_id',
                                                   user_scope_mapping.scope_id)
            db.session.commit()
            return {
                'message': 'User user_scope_mapping updated successfully.',
                'is_successful': True
            }

        # check if the user already has a user_scope_mapping for the scope
        user_scope_mapping_check = UserScopeMapping.query.filter(
            (UserScopeMapping.users.any(id=data.get('user_id', None)) &
             (UserScopeMapping.scope_id == data.get('scope_id',
                                                    None)))).first()
        if user_scope_mapping_check:
            return {
                'message': 'User already has the user_scope_mapping {} for scope ID: {}. Please edit user_scope_mapping ID: {} instead' \
                    .format(user_scope_mapping_check.role, user_scope_mapping_check.scope_id,
                                                                        user_scope_mapping_check.id),
                'user_scope_mapping_id': user_scope_mapping_check.id,
                'is_successful': False
            }

        # Make sure the user has permissions for the scope in question before adding a user_scope_mapping
        if not g.user.can.modify.scope_id(data.get('scope_id', None)):
            return abort(401)
        if not data.get('role', None) in Config.APPROVED_ROLES:
            return {
                'message':
                'Role must be in {}'.format(str(Config.APPROVED_ROLES)),
                'is_successful': False
            }
        user_scope_mapping = UserScopeMapping().create(
            role=data.get('role', None), scope_id=data.get('scope_id', None))
        user.user_scope_mappings.append(user_scope_mapping)
        db.session.commit()

        return {
            'message': 'UserScopeMapping added successfully.',
            'user_scope_mapping_id': user_scope_mapping.id,
            'is_successful': True
        }
Beispiel #13
0
    def post(self, scope_id=None):
        # print(request.values)
        data = request.values

        if data.get('id', scope_id):
            if not g.user.can.modify.scope_id(data.get('id', scope_id)):
                return abort(401)
            scope_check = Scope.query.filter(
                (Scope.name == data.get('name', None)) &
                (Scope.id != data.get('id', scope_id))
            ).first()
            if scope_check:
                return {
                    'message': 'Scope name already in use under scope ID {}'.format(scope_check.id),
                    'is_successful': False
                }
            
            scope = Scope.query.filter_by(id=data.get('id', scope_id)).first()
            if not scope:
                return {
                    'message': 'Scope does not exist',
                    'is_successful': False
                }
            parent_scope = Scope.query.filter_by(id=data.get('parent_scope_id', None)).first()
            if scope == parent_scope:
                return {
                    'message': 'Invalid parent scope',
                    'is_successful': False
                }
            scope.parent_scope_id = data.get('parent_scope_id', scope.parent_scope_id)
            scope.name = data.get('name', scope.name)
            scope.category_id = data.get('category_id', scope.category_id)
            category_check = Category.query.filter_by(id=scope.category_id).first()
            if not category_check:
                return {
                    'message': 'Category does not exist - cannot modify scope',
                    'is_successful': False
                }
            db.session.commit()

            return {
                'message': 'Scope updated successfully.',
                'is_successful': True
            }

        scope_check = Scope.query.filter_by(name=data.get('name', None)).first()
        if scope_check:
            return {
                'message': 'Scope name already in use under scope ID {}'.format(scope_check.id),
                'is_successful': False
            }

        # For new scopes, check to see if the user has access to the parent scope
        # before allowing them to create a new one
        parent_scope = Scope.query.filter_by(id=data.get('parent_scope_id', None)).first()
        if not data.get('parent_scope_id', None) \
            or not parent_scope:
            return {
                'message': 'Invalid parent scope',
                'is_successful': False
            }
        category_check = Category.query.filter_by(id=data.get('category_id', None)).first()
        if not category_check:
            return {
                'message': 'Category does not exist - cannot add scope',
                'is_successful': False
            }
        if not g.user.can.modify.scope_id(data.get('parent_scope_id', None)):
            return abort(401)
        scope = Scope().create(parent_scope_id=data.get('parent_scope_id', None),
                               name=data.get('name', None),
                               category_id=data.get('category_id', None))

        db.session.commit()

        return {
            'message': 'Scope added successfully.',
            'scope_id': scope.id
        }
Beispiel #14
0
    def post(self, user_id=None):

        data = request.values

        if data.get('id', user_id):
            # mobile number is not required so only validate it if it was sent in the request
            if data.get('mobile_number', None):
                try:
                    int(data.get('mobile_number', None))
                except:
                    return {
                        'message': 'Invalid phone number',
                        'is_successful': False
                    }
                try:
                    phone_number = phonenumbers.parse(data.get('mobile_number', None), 
                                                                        Config.DEFAULT_PHONE_REGION)
                    phone_number = phonenumbers.format_number(phone_number, 
                                                                phonenumbers.PhoneNumberFormat.E164)
                except Exception as e:
                    return {
                        'message': str(e),
                        'is_successful': False
                    }
            if data.get('email', None):
                try:
                    validate_email(data.get('email', None))
                except Exception as e:
                    return {
                        'message': 'Email address not valid',
                        'is_successful': False
                    }
            if not hasattr(g, 'user') or not g.user or not g.user.can.modify.user_id(data.get('id', user_id)):
                return abort(401)
            user_name_check = User.query.filter(
                (User.username == data.get('username', None)) &
                (User.id != data.get('id', user_id))
            ).first()
            email_check = User.query.filter(
                (User.email == data.get('email', None)) &
                (User.id != data.get('id', user_id))
            ).first()
            if user_name_check:
                return {
                    'message': 'Username already in use',
                    'is_successful': False
                }
            if email_check:
                return {
                    'message': 'Email already in use',
                    'is_successful': False
                }
            user = User.query.filter_by(id=data.get('id', user_id)).first()
            if not user:
                return {
                    'message': 'User not found',
                    'is_successful': False
                }
            user.first_name = data.get('first_name', user.first_name)
            user.username = data.get('username', user.username)
            user.email = data.get('email', user.email)
            user.mobile_number = data.get('mobile_number', user.mobile_number)
            user.last_name = data.get('last_name', user.last_name)
            if data.get('password', None):
                user.set_password(data.get('password'))
            db.session.commit()

            return {
                'message': 'User updated successfully.',
                'is_successful': True
            }

        user_name_check = User.query.filter_by(username=data.get('username', None)).first()
        email_check = User.query.filter_by(email=data.get('email', None)).first()
        if user_name_check:
            return {         
                'message': 'Username already in use',
                'is_successful': False
            }
        if email_check:
            return {           
                'message': 'Email already in use',
                'is_successful': False
            }

        if data.get('mobile_number') and data.get('username') and \
            data.get('email') and data.get('first_name') and \
            data.get('last_name') and data.get('password'):
            pass
        else:
            return {           
                'message': 'All fields are required',
                'is_successful': False
            }
        if data.get('mobile_number', None):
            try:
                int(data.get('mobile_number', None))
            except:
                return {
                    'message': 'Invalid phone number',
                    'is_successful': False
                }
            try:
                phone_number = phonenumbers.parse(data.get('mobile_number', None), 
                                                            Config.DEFAULT_PHONE_REGION)
                phone_number = phonenumbers.format_number(phone_number, 
                                                          phonenumbers.PhoneNumberFormat.E164)
            except Exception as e:
                return {
                    'message': str(e),
                    'is_successful': False
                }
        else:
            phone_number = None
        if not validate_email(data.get('email', None)):
            return {
                'message': 'Email address not valid',
                'is_successful': False
            }
        user = User().create(first_name=data.get('first_name', None),
                             username=data.get('username', None),
                             mobile_number=phone_number,
                             email=data.get('email', None),
                             last_name=data.get('last_name', None))

        if data.get('password', None):
            user.set_password(data.get('password', None))

        db.session.commit()

        # Generate email verification token
        token = user.generate_auth_token()
        email_body = 'Please use the following token to verify your email: {}' \
            .format(api.url_for(PublicAuthApi, action='verify-email', 
                                        token=token, _external=True))
        send_email('*****@*****.**', 'Please Verify Your Email', user.email, email_body)

        return {     
            'message': 'User added successfully.',
            'user_id': user.id,
            'is_successful': True
        }
Beispiel #15
0
    def post(self, app_context_id=None):
        data = request.values

        if not g.user.is_root_admin:
            return abort(401)

        if data.get('id', app_context_id):
            app_context_check = AppContext.query.filter(
                (AppContext.name == data.get('name', None))
                & (AppContext.id != data.get('id', app_context_id))).first()
            if app_context_check:
                return {
                    'message': 'AppContext name already in use under app_context ID {}' \
                        .format(app_context_check.id),
                    'is_successful': False
                }
            app_context = AppContext.query.filter_by(
                id=data.get('id', app_context_id)).first()
            if not app_context:
                return {
                    'message': 'App context does not exist',
                    'is_successful': False
                }
            app_context.name = data.get('name', app_context.name)
            if data.get('context_credentials', None):
                try:
                    app_context.context_credentials = data.get(
                        'context_credentials', app_context.context_credentials)
                except:
                    return {
                        'message':
                        'Context credentials must be in JSON format',
                        'is_successful': False
                    }
            app_context.application_id = data.get('application_id',
                                                  app_context.application_id)
            db.session.commit()
            return {
                'message': 'AppContext updated successfully.',
                'is_successful': True
            }

        app_context_check = AppContext.query.filter_by(
            name=data.get('name', None)).first()
        if app_context_check:
            return {
                'message':
                'AppContext name already in use under app_context ID {}'.
                format(app_context_check.id),
                'is_successful':
                False
            }
        app_context = AppContext()
        if not data.get('name', None):
            return {'message': 'Context name missing', 'is_successful': False}
        app_context.name = data.get('name', None)
        try:
            app_context.context_credentials = data.get('context_credentials',
                                                       None)
        except:
            return {
                'message': 'Context credentials must be in JSON format',
                'is_successful': False
            }
        if not data.get('application_id', None):
            return {
                'message': 'Application ID missing',
                'is_successful': False
            }
        app_context.application_id = data.get('application_id', None)
        db.session.add(app_context)
        db.session.commit()

        return {
            'message': 'AppContext added successfully.',
            'app_context_id': app_context.id,
            'is_successful': True
        }