Exemple #1
0
def move2trash(file_ids=None):
    """
    Move files to trash
    """
    email = get_email_in_jwt()
    user_id = str(find_one_by_email(email).id)

    if not isinstance(file_ids, list):
        return "Only accept `list` datatype"
    if len(file_ids) == 0:
        return "Nothing to move!"

    parent_of_first_file = files.utils.get_file(file_ids[0])
    parent_of_first_file = parent_of_first_file['parent_id']

    deleting_file = [files.utils.get_file(file_id) for file_id in file_ids]

    for file in deleting_file:
        file_id = file.get("file_id")
        if file.get('parent_id') != parent_of_first_file:
            raise DiffParentException(
                "Can't move files which have different parents")
        user_permission = get_role_of_user(user_id=user_id, file_id=file_id)
        if not user_permission.get('is_owner'):
            raise PermissionException("You can't delete file of another user!")
        if file_id == user_id:
            raise PermissionException("You can't delete your home folder")

    for file_id in file_ids:
        move_one_file_to_trash(file_id)

    return True
 def wrapper(*arg, **kwargs):
     verify_jwt_in_request()
     email = get_jwt_identity()
     if email is None:
         raise PermissionException()
     user = models.User.query.filter_by(email=email).first()
     if user is None:
         raise UserNotFoundException()
     if not user.is_admin:
         raise PermissionException("Admin required")
     return fn(*arg, **kwargs)
 def wrapper(*args, file_id, **kwargs):
     verify_jwt_in_request()
     email = get_jwt_identity()
     if email is None:
         raise PermissionException()
     user = models.User.query.filter_by(email=email).first()
     if user is None:
         raise UserNotFoundException()
     user_permission = get_role_of_user(user_id=user.id, file_id=file_id)
     if user_permission['is_owner'] or user_permission['editable'] or user_permission['viewable']:
         return fn(*args, file_id, **kwargs)
     else:
         raise PermissionException('You are not allowed to view this file!')
 def wrapper(file_id, **kwargs):
     verify_jwt_in_request()
     email = get_jwt_identity()
     if email is None:
         raise PermissionException()
     user = models.User.query.filter_by(email=email).first()
     if user is None:
         raise UserNotFoundException()
     user_permission = get_role_of_user(user_id=user.id, file_id=file_id)
     if user_permission['is_owner']:
         return fn(file_id, **kwargs)
     else:
         raise PermissionException('You must be owner to authorize!')
Exemple #5
0
def check_permission(user_email):
    from .token import check_jwt_token
    jwt_email = check_jwt_token()
    if (jwt_email == None):
        raise NeedLoggedInException()
    if (user_email != jwt_email):
        raise PermissionException()
Exemple #6
0
def drop_out(file_ids):
    """
    Drop away files from ES
    """
    email = get_jwt_identity()
    user_id = find_one_by_email(email).id
    user_id = str(user_id)

    for file_id in file_ids:
        permission = get_role_of_user(user_id, file_id)
        if not permission.get('is_owner'):
            raise PermissionException(
                "You can't delete this files since you are not their owner")
        if file_id == user_id:
            raise PermissionException("You can't delete your home folder")

    file_ids = get_descendants_of_list(file_ids)
    for file_id in file_ids:
        files.delete.delete(file_id)
def viewable_check(file_id, error_message='You are not allowed to view this file!'):
    email = get_email_in_jwt()
    user_id = None
    if email:
        user_id = find_one_by_email(email).id
        user_id = str(user_id) if user_id else user_id
    permission = get_role_of_user_not_trashed(user_id, file_id)
    if permission.get('trashed'):
        raise FileDeletedException()
    if not permission['viewable']:
        raise PermissionException(error_message)
    return permission, {'user_id': user_id, 'email': email}
Exemple #8
0
def share(args):
    try:
        email = get_jwt_identity()
        args['user_id'] = str(find_one_by_email(email).id)
    except Exception as e:
        _logger.error(e)
        raise UserNotFoundException()

    file_id = args['file_id']
    file = get_file(file_id)
    if not file:
        raise FileNotExistException()

    if (file['owner'] != args['user_id']):
        raise PermissionException("You are not the owner of this file/folder")

    if args.get('private'):
        share_mode = 0
        return update.update(file_id, share_mode=share_mode,
                             users_shared=[]).get('result')  # private
    elif args.get('emails'):
        share_mode = 1
        users_shared = []
        for email in args['emails']:
            user_shared = find_one_by_email(email)
            if not user_shared:
                raise UserNotFoundException("User with email " + email +
                                            " not exist!!!")
            users_shared.append(user_shared.id)
        for user_id in users_shared:
            from file_management.services.notification import create_notification
            create_notification(owner=int(args.get('user_id')),
                                viewed=False,
                                user_id=user_id,
                                file_id=file.get('file_id'))
        users_shared = [str(id) for id in users_shared]
        return update.update(file_id,
                             share_mode=share_mode,
                             users_shared=users_shared).get('result')  # custom
    elif args.get('share_by_link'):
        share_mode = 2
        return update.update(file_id, share_mode=share_mode,
                             users_shared=[]).get('result')  # public
Exemple #9
0
 def build_filter_condions(self, args):
     must_conditions = []
     must_conditions.append(
         query.Bool(should=[
             query.Term(trashed=False),
             query.Bool(must_not=query.Exists(field="trashed"))
         ] if not args.get('trash') else [query.Term(trashed=True)]))
     if not args.get('is_folder_api'):
         if args.get('file_id'):
             should_conditions = []
             should_conditions.append(query.Term(share_mode={'value': 2}))
             if args.get('user_id'):
                 should_conditions.append(
                     query.Term(owner=args.get('user_id')))
                 should_conditions.append(
                     self.shared_by_email_permission_condition(args))
             must_conditions.append(
                 query.Bool(should=should_conditions,
                            minimum_should_match=1))
         elif not args.get('user_id'):
             raise PermissionException("You must login to use this api")
         elif args.get('share'):
             must_conditions.append(
                 self.shared_by_email_permission_condition(args))
         elif args.get('q'):
             must_conditions.append(
                 query.Bool(should=[
                     query.Term(owner=args.get('user_id')),
                     self.shared_by_email_permission_condition(args),
                 ],
                            minimum_should_match=1))
         else:
             must_conditions.append(query.Term(owner=args.get('user_id')))
     if args.get('star'):
         must_conditions.append(query.Term(star=True))
     if args.get('only_photo'):
         must_conditions.append(query.Prefix(file_type={'value': 'image'}))
     return query.Bool(must=must_conditions)
 def wrapper(*arg, **kwargs):
     verify_jwt_in_request()
     email = get_jwt_identity()
     if email is None:
         raise PermissionException("Login required")
     return fn(*arg, **kwargs)
def check_insert_privilege(user_id, parent_id):
    privileges = get_role_of_user(user_id=user_id, file_id=parent_id)
    if not (privileges['is_owner'] and privileges['editable']):
        raise PermissionException('You are not allowed to insert file into this folder')