Ejemplo n.º 1
0
 public virtual void CreateSpecialPermission(SpecialPermission permission)
 {
     using (var session = GetSession())
     {
         session.Save(permission);
     }
 }
Ejemplo n.º 2
0
        public bool UserCheckPermission(int userId, int permissionId)
        {
            var user       = UserRepository.UserGetById(userId);
            var permission = PermissionRepository.PermissionGetById(permissionId);
            SpecialPermission specialPermission = null;

            if (permission != null)
            {
                specialPermission = SpecialPermissionRepository.SpecialPermissionGetByUserIdAndPermissionName(user.Id, permission.Name);
            }
            if (user != null && UserCheckRole(user.Id, (int)Roles.Administrator))
            {
                return(true);
            }
            if (user == null || permission == null)
            {
                return(false);
            }
            if (specialPermission != null)
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 3
0
 protected void btnSave_Click(object sender, EventArgs e)
 {
     foreach (RepeaterItem item in rptPermissions.Items)
     {
         CheckBox    chkPermission = (CheckBox)item.FindControl("chkPermission");
         HiddenField hiddenName    = (HiddenField)item.FindControl("hiddenName");
         if (_role != null)
         {
             if (_permissions.Contains(hiddenName.Value) && !chkPermission.Checked) // Nếu có quyền và không có check
             {
                 SpecialPermission permission = Module.PermissionGetByRole(hiddenName.Value, _role);
                 if (permission != null)
                 {
                     Module.Delete(permission);
                 }
             }
             else if (!_permissions.Contains(hiddenName.Value) && chkPermission.Checked)
             {
                 SpecialPermission permission = Module.PermissionGetByRole(hiddenName.Value, _role);
                 if (permission == null)
                 {
                     permission            = new SpecialPermission();
                     permission.Name       = hiddenName.Value;
                     permission.Role       = _role;
                     permission.ModuleType = Section.ModuleType;
                     Module.SaveOrUpdate(permission);
                 }
             }
         }
         else
         {
             if (chkPermission.Enabled)// Phải enable, tức là quyền theo user chứ không phải theo role
             {
                 if (_permissions.Contains(hiddenName.Value) && !chkPermission.Checked)
                 // Nếu có quyền và không có check
                 {
                     SpecialPermission permission = Module.PermissionGetByUser(hiddenName.Value, _user);
                     if (permission != null)
                     {
                         Module.Delete(permission);
                     }
                 }
                 else if (!_permissions.Contains(hiddenName.Value) && chkPermission.Checked)
                 {
                     SpecialPermission permission = Module.PermissionGetByUser(hiddenName.Value, _user);
                     if (permission == null)
                     {
                         permission            = new SpecialPermission();
                         permission.Name       = hiddenName.Value;
                         permission.User       = _user;
                         permission.ModuleType = Section.ModuleType;
                         Module.SaveOrUpdate(permission);
                     }
                 }
             }
         }
     }
     PageRedirect(string.Format("SetPermission.aspx?NodeId={0}&SectionId={1}", Node.Id, Section.Id));
 }
Ejemplo n.º 4
0
        public virtual IList <SpecialPermission> GetSpecialPermissions()
        {
            using (var session = GetSession())
            {
                SpecialPermission specPermissionAlias = null;

                var permissions = session.QueryOver(() => specPermissionAlias)
                                  .List <SpecialPermission>();

                return(permissions);
            }
        }
Ejemplo n.º 5
0
        public virtual IList <SpecialPermission> GetSpecialPermissionsByIds(IEnumerable <int> specialPermissionIds)
        {
            using (var session = GetSession())
            {
                SpecialPermission specPermissionAlias = null;

                var permissions = session.QueryOver(() => specPermissionAlias)
                                  .AndRestrictionOn(() => specPermissionAlias.Id).IsInG(specialPermissionIds)
                                  .List <SpecialPermission>();

                return(permissions);
            }
        }
Ejemplo n.º 6
0
        public async Task <UserPermissionResponse> CreateUserPermission(DirectUserPermissionModel model, int companyId)
        {
            using (var _context = _dbcontext())
            {
                try
                {
                    var userPerm = await _context.SpecialPermissions.Where(x => x.Is_deleted == false && x.CompanyId == companyId).ToListAsync();

                    for (int i = 0; i < model.UserPerms.Length; i++)
                    {
                        var is_user_perm_exist = userPerm.Any(x => x.UserId == model.UserPerms[i].UserId && x.DocumentType == model.UserPerms[i].Permission.DocumentType);
                        if (is_user_perm_exist == true)
                        {
                            model.UserPerms[i].Success = false;
                            model.UserPerms[i].Message = "User Permission Exist";
                        }
                        else
                        {
                            var perm = new SpecialPermission {
                                UserId              = model.UserPerms[i].UserId,
                                DocumentType        = model.UserPerms[i].Permission.DocumentType,
                                DocumentAccessLevel = model.UserPerms[i].Permission.DocumentAccessLevel,
                                Create              = model.UserPerms[i].Permission.Create,
                                Read          = model.UserPerms[i].Permission.Read,
                                Update        = model.UserPerms[i].Permission.Update,
                                Delete        = model.UserPerms[i].Permission.Delete,
                                Upload        = model.UserPerms[i].Permission.Upload,
                                Download      = model.UserPerms[i].Permission.Download,
                                Amend         = model.UserPerms[i].Permission.Amend,
                                Cancel        = model.UserPerms[i].Permission.Cancel,
                                Approval      = model.UserPerms[i].Permission.Approval,
                                SetPermission = model.UserPerms[i].Permission.SetPermission,
                                CompanyId     = companyId,
                                DateCreated   = DateTime.Now
                            };
                            model.UserPerms[i].Success = true;
                            model.UserPerms[i].Message = "Created";
                            _context.SpecialPermissions.Add(perm);
                        }
                    }
                    await _context.SaveChangesAsync();

                    return(new UserPermissionResponse(model));
                }
                catch (Exception ex)
                {
                    return(new UserPermissionResponse(ex.Message));
                }
            }
        }
Ejemplo n.º 7
0
        public override async Task <PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
        {
            //CommandInfo can be null!

            if (!(context.User is SocketGuildUser user) || !(context.Guild is SocketGuild server))
            {
                return(PreconditionResult.FromError("You must be in a server to use this."));
            }

            if (specialPermission.HasValue)
            {
                SpecialPermission thisValue = 0;

                if (server.OwnerId == user.Id)
                {
                    thisValue |= SpecialPermission.Owner;
                    thisValue |= SpecialPermission.Admin;
                }
                else if (server.Roles.Any(r => r.Permissions.Administrator))
                {
                    thisValue |= SpecialPermission.Admin;
                }

                if (user.IsBotMaster())
                {
                    thisValue |= SpecialPermission.BotMaster;
                }

                if (((byte)thisValue & (byte)specialPermission.Value) > 0)
                {
                    return(PreconditionResult.FromSuccess());
                }
            }

            if (requireAny != null && requireAny.Length > 0)
            {
                if (user.HasAnyPermissions(requireAny))
                {
                    return(PreconditionResult.FromSuccess());
                }

                return(PreconditionResult.FromError(
                           requireAny.Length > 1
                                                ? $"Missing one of the following permissions:\r\n{string.Join("\r\n", requireAny.Select(s => "`" + s + "`"))}"
                                                : $"Missing permission: `{requireAny[0]}`."
                           ));
            }

            return(PreconditionResult.FromError("You do not have a permission to use this."));
        }
Ejemplo n.º 8
0
        public virtual IList <Group> GetGroupsBySpecialPermissionIds(IEnumerable <int> specialPermissionIds)
        {
            using (var session = GetSession())
            {
                Group             groupAlias             = null;
                SpecialPermission specialPermissionAlias = null;

                var groups = session.QueryOver(() => groupAlias)
                             .JoinQueryOver(x => groupAlias.SpecialPermissions, () => specialPermissionAlias)
                             .AndRestrictionOn(() => specialPermissionAlias.Id).IsInG(specialPermissionIds)
                             .List <Group>();

                return(groups);
            }
        }
Ejemplo n.º 9
0
        public virtual IList <SpecialPermission> GetSpecialPermissionsByGroup(int groupId)
        {
            using (var session = GetSession())
            {
                SpecialPermission specPermissionAlias = null;
                Group             groupAlias          = null;

                var permissions = session.QueryOver(() => specPermissionAlias)
                                  .JoinQueryOver(x => specPermissionAlias.Groups, () => groupAlias)
                                  .Where(() => groupAlias.Id == groupId)
                                  .List <SpecialPermission>();

                return(permissions);
            }
        }
Ejemplo n.º 10
0
        public virtual IList <SpecialPermission> GetSpecialPermissionsByUserAndType(int userId, SpecialPermissionCategorization type)
        {
            using (var session = GetSession())
            {
                SpecialPermission specPermissionAlias = null;
                Group             groupAlias          = null;
                User userAlias = null;

                var permissions = session.QueryOver(() => specPermissionAlias)
                                  .JoinQueryOver(x => specPermissionAlias.Groups, () => groupAlias)
                                  .JoinQueryOver(x => groupAlias.Users, () => userAlias)
                                  .Where(() => userAlias.Id == userId)
                                  .And(() => specPermissionAlias.PermissionCategorization == type)
                                  .List <SpecialPermission>();

                return(permissions);
            }
        }
Ejemplo n.º 11
0
 public RequirePermissionAttribute(SpecialPermission specialPermission, params string[] requireAny)
 {
     this.specialPermission = specialPermission;
     this.requireAny        = requireAny;
 }