/// <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;
        }
        /// <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);
        }