Example #1
0
        public bool HasPermissions(int entityId, int userId, ENUMPermissionType permissionType)
        {
            SqlParameter userParam       = new SqlParameter("userId", userId);
            SqlParameter entityParam     = new SqlParameter("entityId", entityId);
            SqlParameter permissionParam = new SqlParameter("permissionType", permissionType);

            bool hasPermissions = this.context.Permissions.FromSqlRaw(@"
					WITH treeList AS
						(SELECT tree.Id, tree.ParentId, 1 AS treeLevel
						FROM [Tree_Entities] tree
						WHERE tree.Id=@entityId

						UNION ALL

						SELECT parents.Id, Parents.ParentId, TL.treeLevel + 1 AS treeLevel
						FROM [Tree_Entities] parents
						INNER JOIN treeList AS TL
						ON parents.Id = TL.ParentId
						)
					SELECT treeList.Id as EntityId, perms.Id as Id, perms.UserId as UserId, perms.PermissionType as PermissionType, treeList.treeLevel FROM treeList, [Permissions] perms
					WHERE treeList.Id=perms.EntityId and perms.UserId=@userId and PermissionType=@permissionType
					"                    , new SqlParameter[] { userParam, entityParam, permissionParam }).AsEnumerable().Any();

            return(hasPermissions);
        }
Example #2
0
        public bool AddPermission(int userId, int entityId, ENUMPermissionType permissionType)
        {
            User       user   = this.unitOfWork.UserRepository.GetByID(userId);
            TreeEntity entity = this.unitOfWork.TreeRepository.GetByID(entityId);

            if (user == null || entity == null)
            {
                throw new InvalidParametersException();
            }

            Permission permission = this.unitOfWork.PermissionRepository.Get(permission => permission.Entity.Id == entityId && permission.User.Id == userId).FirstOrDefault();

            if (permission == null)
            {
                this.unitOfWork.PermissionRepository.Insert(new Permission {
                    User = user, Entity = entity, PermissionType = permissionType
                });
                this.unitOfWork.Save();
            }
            else
            {
                throw new ObjectAlreadyExistsException();
            }

            return(true);
        }
Example #3
0
        public bool UpdatePermission(int permissionId, ENUMPermissionType permissionType)
        {
            Permission permission = this.unitOfWork.PermissionRepository.GetByID(permissionId);

            if (permission == null)
            {
                throw new ObjectDoesNotExistException();
            }
            permission.PermissionType = permissionType;

            this.unitOfWork.PermissionRepository.Update(permission);
            this.unitOfWork.Save();

            return(true);
        }
        public async Task <ActionResult <Response <bool> > > UpdatePermissionAsync(int permissionId, ENUMPermissionType permissionType)
        {
            try
            {
                var authorizationResult = await authorizationService.AuthorizeAsync(User, permissionsBL.GetPermission(permissionId).Entity, new OwnerRequirement());

                if (authorizationResult.Succeeded)
                {
                    return(new Response <bool>(permissionsBL.UpdatePermission(permissionId, permissionType)));
                }
                else
                {
                    return(new ForbidResult());
                }
            }
            catch (Exception ex)
            {
                return(new Response <bool>(false, ex));
            }
        }