Esempio n. 1
0
 /// <summary>
 /// Get all permission values from all permissions.
 /// </summary>
 /// <returns></returns>
 public static string[] GetAllPermissionValues()
 {
     return(AllPermissions.Select(p => p.Value).ToArray());
 }
Esempio n. 2
0
        private Result AddWildcardPermission(SocketRole role, string perm)
        {
            var allSplitPerms = AllPermissions.Select(x => x.Split('.'));
            var splitPerm     = perm.Split('.');

            if (splitPerm.All(x => x != "*"))
            {
                return new Result {
                           Type = ResultType.Fail, Message = "Invalid permission format."
                }
            }
            ;                                                                                                                       // Catch foo.bar*
            if (splitPerm.Length > 2)
            {
                return new Result {
                           Type = ResultType.Fail, Message = "Invalid permission format."
                }
            }
            ;                                                                                                               // Catch *.foo.bar
            var  parent     = splitPerm[0];
            var  child      = splitPerm[1];
            bool setCreated = false;

            if (!Permissions.ContainsKey(role.Id))
            {
                Permissions.Add(role.Id, new HashSet <string>());

                setCreated = true;
            }
            if (parent == "*")
            {
                if (child == "*")
                {
                    // Add all existing permissions for all parents and children
                    foreach (var p in AllPermissions)
                    {
                        Permissions[role.Id].Add(p);
                    }
                    return(new Result {
                        Type = ResultType.Success, Message = setCreated ? "Created permission set for role and added all existing permissions."
                                                                                        : "Added all existing permissions to role."
                    });
                }
                else
                {
                    // Add all child permission matches for all existing parent permissions
                    foreach (var p in allSplitPerms.Where(x => x[1] == child))
                    {
                        Permissions[role.Id].Add(AllPermissions.FirstOrDefault(x => x == $"{p[0]}.{p[1]}"));
                    }
                    return(new Result {
                        Type = ResultType.Success, Message = setCreated ? $"Created permission set for role and added all child permissions matching `{child}`."
                                                                                        : $"Added all child permissions `{child}` to role."
                    });
                }
            }
            else
            {
                // Add all child permissions for the given parent permission
                foreach (var p in allSplitPerms.Where(x => x[0] == parent))
                {
                    Permissions[role.Id].Add(AllPermissions.FirstOrDefault(x => x == $"{p[0]}.{p[1]}"));
                }
                return(new Result {
                    Type = ResultType.Success, Message = setCreated ? $"Created permission set for role and added all child permissions of `{parent}`."
                                                                                    : $"Added all child permissions of `{parent}` to role."
                });
            }
        }