コード例 #1
0
        /// <summary>
        /// Get role rights
        /// </summary>
        /// <param name="role">Role</param>
        /// <returns>Collection of rights</returns>
        public IEnumerable <Group> GetRoleRights(string role)
        {
            if (!Security.IsAuthorizedTo(BlogEngine.Core.Rights.ViewRoles))
            {
                throw new System.UnauthorizedAccessException();
            }

            var groups = new List <Group>();

            // store the category for each Rights.
            var rightCategories = new Dictionary <BlogEngine.Core.Rights, string>();
            var roleRights      = BlogEngine.Core.Right.GetRights(role);

            foreach (FieldInfo fi in typeof(BlogEngine.Core.Rights).GetFields(BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public))
            {
                BlogEngine.Core.Rights right = (BlogEngine.Core.Rights)fi.GetValue(null);

                if (right != BlogEngine.Core.Rights.None)
                {
                    RightDetailsAttribute rightDetails = null;

                    foreach (Attribute attrib in fi.GetCustomAttributes(true))
                    {
                        if (attrib is RightDetailsAttribute)
                        {
                            rightDetails = (RightDetailsAttribute)attrib;
                            break;
                        }
                    }

                    var category = rightDetails == null ? RightCategory.General : rightDetails.Category;

                    var group = groups.Where(g => g.Title == category.ToString()).FirstOrDefault();

                    var prm = new Permission();
                    var rt  = Right.GetRightByName(right.ToString());

                    prm.Id        = right.ToString();
                    prm.Title     = rt.DisplayName;
                    prm.IsChecked = roleRights.Contains(rt);

                    if (group == null)
                    {
                        var newGroup = new Group(category.ToString());
                        newGroup.Permissions.Add(prm);
                        groups.Add(newGroup);
                    }
                    else
                    {
                        group.Permissions.Add(prm);
                    }
                }
            }

            return(groups);
        }
コード例 #2
0
        public async Task <IEnumerable <Group> > GetRoleRights(string name)
        {
            var role = await _roleMgr.FindByNameAsync(name);

            var groups = new List <Group>();
            // store the category for each Rights.
            var rightCategories = new Dictionary <Rights, string>();
            var roleRights      = await _roleMgr.GetClaimsAsync(role);

            foreach (FieldInfo fi in typeof(Rights).GetFields(BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public))
            {
                var right = (Rights)fi.GetValue(null);

                if (right != Rights.None)
                {
                    RightDetailsAttribute rightDetails = null;

                    foreach (Attribute attrib in fi.GetCustomAttributes(true))
                    {
                        if (attrib is RightDetailsAttribute)
                        {
                            rightDetails = (RightDetailsAttribute)attrib;
                            break;
                        }
                    }

                    var category = rightDetails == null ? RightCategory.General : rightDetails.Category;

                    var group = groups.FirstOrDefault(g => g.Title == category.ToString());

                    var prm = new Permission();
                    var rt  = Right.GetRightByName(right.ToString());

                    prm.Id        = right.ToString();
                    prm.Title     = rt.DisplayName;
                    prm.IsChecked = roleRights.FirstOrDefault(m => m.Value == rt.FlagName) != null;

                    if (group == null)
                    {
                        var newGroup = new Group(category.ToString());
                        newGroup.Permissions.Add(prm);
                        groups.Add(newGroup);
                    }
                    else
                    {
                        group.Permissions.Add(prm);
                    }
                }
            }

            return(groups);
        }
コード例 #3
0
        protected string GetRightsJson()
        {
            var role = this.roleName;

            if (Utils.StringIsNullOrWhitespace(role))
            {
                return("null");
            }
            else
            {
                // outer key is Category (RightCategory)
                // inner key is the Right name
                var jsonDict = new Dictionary <string, Dictionary <string, bool> >();

                // store the category for each Rights.
                var rightCategories = new Dictionary <BlogEngine.Core.Rights, string>();

                foreach (FieldInfo fi in typeof(BlogEngine.Core.Rights).GetFields(BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public))
                {
                    BlogEngine.Core.Rights right = (BlogEngine.Core.Rights)fi.GetValue(null);
                    if (right != BlogEngine.Core.Rights.None)
                    {
                        RightDetailsAttribute rightDetails = null;

                        foreach (Attribute attrib in fi.GetCustomAttributes(true))
                        {
                            if (attrib is RightDetailsAttribute)
                            {
                                rightDetails = (RightDetailsAttribute)attrib;
                                break;
                            }
                        }

                        RightCategory category = rightDetails == null ? RightCategory.General : rightDetails.Category;
                        rightCategories.Add(right, category.ToString());
                    }
                }

                foreach (var right in BlogEngine.Core.Right.GetAllRights())
                {
                    // The None flag isn't meant to be set specifically, so
                    // don't render it out.
                    if (right.Flag != BlogEngine.Core.Rights.None)
                    {
                        if (rightCategories.ContainsKey(right.Flag))
                        {
                            string categoryName = rightCategories[right.Flag];

                            if (!jsonDict.ContainsKey(categoryName))
                            {
                                jsonDict.Add(categoryName, new Dictionary <string, bool>());
                            }

                            jsonDict[categoryName].Add(right.DisplayName, right.Roles.Contains(role));
                        }
                    }
                }

                return(Utils.ConvertToJson(jsonDict));
            }
        }