示例#1
0
        public override bool Equals(object obj)
        {
            GlymaSecurityGroup gsg = obj as GlymaSecurityGroup;

            if (gsg != null)
            {
                if (gsg.GroupId == this.GroupId)
                {
                    return(true);
                }
            }
            return(false);
        }
        internal Group GetGroup(GlymaSecurityGroup securityGroup)
        {
            Group result = null;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (IGlymaSession glymaSession = new WebAppSPGlymaSession(this.WebUrl))
                {
                    using (IDbConnectionAbstraction connectionAbstraction = glymaSession.ConnectionFactory.CreateSecurityDbConnection())
                    {
                        using (SecurityServiceDataContext dataContext = new SecurityServiceDataContext(connectionAbstraction.Connection))
                        {
                            var groups = from g in dataContext.Groups
                                         where g.GroupId == securityGroup.GroupId
                                         && g.SecurableContextId == securityGroup.SecurableContextId
                                         select g;

                            Group group = null; //default value if it doesn't exist
                            if (groups.Any())
                            {
                                group = groups.First();
                            }
                            result = group;
                        }
                    }
                }
            });
            return result;
        }
        /// <summary>
        /// Returns a list presenting the SharePoint Security Groups for the current web that have a specified permission associated with them
        /// </summary>
        /// <param name="webUrl">The URL for the SP site</param>
        /// <param name="permissionLevel">The permission level the groups must have</param>
        /// <returns>A list of groups (wrapped by a ResponseObject)</returns>
        internal GetSecurityGroupsResponse GetSecurityGroups(GlymaPermissionLevel permissionLevel)
        {
            GetSecurityGroupsResponse result = new GetSecurityGroupsResponse() { HasError = false };
            IList<GlymaSecurityGroup> results = new List<GlymaSecurityGroup>();

            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(WebUrl))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPRoleDefinition roleDefinition = null;
                            try
                            {
                                // Check if the role exists, if it does a definition will exist
                                roleDefinition = web.RoleDefinitions[GlymaPermissionLevelHelper.GetPermissionLevelName(permissionLevel)];
                            }
                            catch (Exception)
                            {
                                //if unable to find the role definition it will throw an exception
                            }

                            if (roleDefinition != null)
                            {
                                SPRoleAssignmentCollection roleAssignments = web.RoleAssignments;
                                foreach (SPRoleAssignment roleAssignment in roleAssignments)
                                {
                                    bool hasRoleDefinition = false;
                                    foreach (
                                        SPRoleDefinition definition in roleAssignment.RoleDefinitionBindings)
                                    {
                                        if (definition.Id == roleDefinition.Id)
                                        {
                                            //The role exists for this role assignment
                                            hasRoleDefinition = true;
                                            break;
                                        }
                                    }

                                    if (hasRoleDefinition)
                                    {
                                        SPGroup group = roleAssignment.Member as SPGroup;
                                        //we only want to look at groups
                                        if (group != null)
                                        {
                                            GlymaSecurityGroup glymaGroup = new GlymaSecurityGroup();
                                            glymaGroup.DisplayName = group.Name;

                                            SecurableContext securableContext = this.GetSecurableContext();
                                            glymaGroup.SecurableContextId = securableContext.SecurableContextId;

                                            GlymaSecurityGroupContext sgc = new GlymaSecurityGroupContext(this, securableContext.SecurableContextId, group.ID, web.ID);
                                            Group glGroup = sgc.GetGroup(group.Name);
                                            if (glGroup == null)
                                            {
                                                glGroup = sgc.CreateGroup(group.Name);
                                            }
                                            if (glGroup != null)
                                            {
                                                glymaGroup.GroupId = glGroup.GroupId;
                                                results.Add(glymaGroup);
                                            }
                                            else
                                            {
                                                result.HasError = true;
                                                result.ErrorMessage = "Failed to create the Group in the Glyma Security Database.";
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                results = new List<GlymaSecurityGroup>(); //there was no role by this name, it has no groups
                            }
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                //If an error occurs getting the group listing return no groups
                result.HasError = true;
                result.ErrorMessage = ex.Message;

            }
            if (!result.HasError)
            {
                result.Result = results;
            }
            return result;
        }
        internal GlymaPermissionLevel GetGroupsPermissionLevel(GlymaSecurityGroup glGroup)
        {
            GlymaPermissionLevel result = GlymaPermissionLevel.None;

            IList<GlymaSecurityGroup> pmGroups = _groups[GlymaPermissionLevel.GlymaProjectManager];
            IList<GlymaSecurityGroup> mmGroups = _groups[GlymaPermissionLevel.GlymaMapManager];
            IList<GlymaSecurityGroup> maGroups = _groups[GlymaPermissionLevel.GlymaMapAuthor];
            IList<GlymaSecurityGroup> mrGroups = _groups[GlymaPermissionLevel.GlymaMapReader];

            foreach (GlymaSecurityGroup spGroup in pmGroups)
            {
                if (spGroup.GroupId == glGroup.GroupId)
                {
                    result = GlymaPermissionLevel.GlymaProjectManager;
                    return result; // group has project manager permissions
                }
            }
            foreach (GlymaSecurityGroup spGroup in mmGroups)
            {
                if (spGroup.GroupId == glGroup.GroupId)
                {
                    result = GlymaPermissionLevel.GlymaMapManager;
                    return result; // group has map manager permissions
                }
            }
            foreach (GlymaSecurityGroup spGroup in maGroups)
            {
                if (spGroup.GroupId == glGroup.GroupId)
                {
                    result = GlymaPermissionLevel.GlymaMapAuthor;
                    return result; // group has map author permissions
                }
            }
            foreach (GlymaSecurityGroup spGroup in mrGroups)
            {
                if (spGroup.GroupId == glGroup.GroupId)
                {
                    result = GlymaPermissionLevel.GlymaMapReader;
                    return result; // group has map reader permissions
                }
            }
            return result;
        }
        /// <summary>
        /// Checks if the the group appears in a higher permission level
        /// </summary>
        /// <param name="group"></param>
        /// <param name="currentPermissionLevel"></param>
        /// <returns>True if the group is in a higher level</returns>
        private bool ExistsInHigherPermissionLevel(GlymaSecurityGroup group, GlymaPermissionLevel currentPermissionLevel)
        {
            bool result = false;

            IList<GlymaSecurityGroup> projectManagerGroups = _groups[GlymaPermissionLevel.GlymaProjectManager];
            IList<GlymaSecurityGroup> mapManagersGroups = _groups[GlymaPermissionLevel.GlymaMapManager];
            IList<GlymaSecurityGroup> mapAuthorGroups = _groups[GlymaPermissionLevel.GlymaMapAuthor];
            IList<GlymaSecurityGroup> oldMapAuthorsGroups = _groups[GlymaPermissionLevel.GlymaMapAuthorOld];

            switch (currentPermissionLevel)
            {
                case GlymaPermissionLevel.GlymaMapManager:
                    foreach (GlymaSecurityGroup securityGroup in projectManagerGroups)
                    {
                        if (group.GroupId == securityGroup.GroupId)
                        {
                            result = true;
                            break; //found match, break foreach loop early
                        }
                    }
                    break;
                case GlymaPermissionLevel.GlymaMapAuthor:
                    foreach (GlymaSecurityGroup securityGroup in mapManagersGroups)
                    {
                        if (group.GroupId == securityGroup.GroupId)
                        {
                            result = true;
                            break; //found match, break foreach loop early
                        }
                    }
                    foreach (GlymaSecurityGroup securityGroup in projectManagerGroups)
                    {
                        if (group.GroupId == securityGroup.GroupId)
                        {
                            result = true;
                            break; //found match, break foreach loop early
                        }
                    }
                    break;
                case GlymaPermissionLevel.GlymaMapReader:
                    foreach (GlymaSecurityGroup securityGroup in mapAuthorGroups)
                    {
                        if (group.GroupId == securityGroup.GroupId)
                        {
                            result = true;
                            break; //found match, break foreach loop early
                        }
                    }
                    foreach (GlymaSecurityGroup securityGroup in oldMapAuthorsGroups)
                    {
                        if (group.GroupId == securityGroup.GroupId)
                        {
                            result = true;
                            break; //found match, break foreach loop early
                        }
                    }
                    foreach (GlymaSecurityGroup securityGroup in mapManagersGroups)
                    {
                        if (group.GroupId == securityGroup.GroupId)
                        {
                            result = true;
                            break; //found match, break foreach loop early
                        }
                    }
                    foreach (GlymaSecurityGroup securityGroup in projectManagerGroups)
                    {
                        if (group.GroupId == securityGroup.GroupId)
                        {
                            result = true;
                            break; //found match, break foreach loop early
                        }
                    }
                    break;
            }
            return result;
        }
 internal GlymaSecurityAssociationContext(SecurityContextManager context, GlymaSecurityGroup group, GlymaSecurableObject securableObject)
 {
     Group = group;
     SecurableObject = securableObject;
     Context = context;
 }