コード例 #1
0
ファイル: User.cs プロジェクト: emmaburstow/Umbraco-CMS
        void IUser.RemoveAllowedSection(string sectionAlias)
        {
            //don't do anything if they aren't allowed it already
            if (AllowedSections.Contains(sectionAlias) == false)
            {
                return;
            }

            var groups = Groups.ToArray();
            //our only option here is to check if a custom group is created for this user, if so we can remove it from that group, otherwise we'll throw
            //now we'll check if the user has a special 1:1 user group created for itself. This will occur if this method is used and also during an upgrade.
            //this comes in the alias form of userName + 'Group'
            var customUserGroup = groups.FirstOrDefault(x => x.Alias == (Username + "Group"));

            if (customUserGroup != null)
            {
                //if the group isn't IUserGroup we'll need to look it up
                var realGroup = customUserGroup as IUserGroup ?? ApplicationContext.Current.Services.UserService.GetUserGroupById(customUserGroup.Id);
                realGroup.RemoveAllowedSection(sectionAlias);
                //now we need to flag this for saving (hack!)
                GroupsToSave.Add(realGroup);
            }
            else
            {
                throw new InvalidOperationException("Cannot remove the allowed section using this obsolete API. Modify the user's groups instead");
            }
        }
コード例 #2
0
ファイル: User.cs プロジェクト: emmaburstow/Umbraco-CMS
        void IUser.AddAllowedSection(string sectionAlias)
        {
            //don't do anything if they are allowed it already
            if (AllowedSections.Contains(sectionAlias))
            {
                return;
            }

            //This is here for backwards compat only.
            //First we'll check if the user is part of the 'admin' group. If so then we can ensure that the admin group has this section available to it.
            //otherwise, the only thing we can do is create a custom user group for this user and add this section.
            //We are checking for admin here because if the user is an admin and an allowed section is being added, then it's assumed it's to be added
            //for the whole admin group (i.e. Forms installer does this for admins)
            var groups = Groups.ToArray();
            var admin  = groups.FirstOrDefault(x => x.Alias == Constants.Security.AdminGroupAlias);

            if (admin != null)
            {
                //if the group isn't IUserGroup we'll need to look it up
                var realGroup = admin as IUserGroup ?? ApplicationContext.Current.Services.UserService.GetUserGroupById(admin.Id);
                realGroup.AddAllowedSection(sectionAlias);
                //now we need to flag this for saving (hack!)
                GroupsToSave.Add(realGroup);
            }
            else
            {
                //now we'll check if the user has a special 1:1 user group created for itself. This will occur if this method is used and also during an upgrade.
                //this comes in the alias form of userName + 'Group'
                var customUserGroup = groups.FirstOrDefault(x => x.Alias == (Username + "Group"));
                if (customUserGroup != null)
                {
                    //if the group isn't IUserGroup we'll need to look it up
                    var realGroup = customUserGroup as IUserGroup ?? ApplicationContext.Current.Services.UserService.GetUserGroupById(customUserGroup.Id);
                    realGroup.AddAllowedSection(sectionAlias);
                    //now we need to flag this for saving (hack!)
                    GroupsToSave.Add(realGroup);
                }

                //ok, so the user doesn't have a 1:1 group, we'll need to flag it for creation
                var newUserGroup = new UserGroup
                {
                    Alias = Username + "Group",
                    Name  = "Group for " + Username
                };
                newUserGroup.AddAllowedSection(sectionAlias);
                //add this user to this new group
                AddGroup(newUserGroup);
                GroupsToSave.Add(newUserGroup);
            }
        }