예제 #1
0
        /// <summary>
        /// Add role to CMS.
        /// </summary>
        /// <param name="role">Role to add</param>
        /// <returns>ID of added role</returns>
        public long? AddRole(Role role)
        {
            if (role != null)
            {
                try
                {
                    if (RoleExists(role.Id))
                    {
                        LogMessage("Role " + role.CodeName + " was not added because it already exists.");
                    }
                    else
                    {
                        // Send REST request
                        var response = Provider.MakeRequest(BaseUrl + "/rest/cms.role", HttpVerb.Post, null,
                            role.ToString());

                        var id = Convert.ToInt64(RestHelper.GetAttributeFromReponse(response, role.IdTagName));

                        LogMessage("Role " + role.CodeName + " has been added.");

                        if (OnSuccess != null)
                        {
                            OnSuccess();
                        }

                        return id;
                    }

                }
                catch (Exception ex)
                {
                    LogError("Adding role " + role.CodeName + " failed.", ex);
                }

            }

            return null;
        }
예제 #2
0
        /// <summary>
        /// Handle single role.
        /// </summary>
        /// <param name="entry">Group LDAP object.</param>
        private void HandleGroup(SearchResultEntry entry)
        {
            // Create CMS object from LDAP object
            var role = new Role(LdapHelper.GetObjectGuid(entry),
                LdapHelper.GetAttributeString(entry.Attributes["sAMAccountName"], true),
                LdapHelper.GetAttributeString(entry.Attributes["displayName"]),
                GroupBindings.Select(k => new KeyValuePair<string, string>(k.Cms, LdapHelper.GetAttributeString(entry.Attributes[k.Ldap]))).ToList());

            var existing = Replica.Groups.FirstOrDefault(g => g.Guid == role.Guid);

            List<User> currentMembers = (existing == null) ? new List<User>() : Replica.Bindings.Where(b => b.RoleId == existing.Id).SelectMany(b => Replica.Users.Where(u => u.Id == b.UserId)).ToList();
            List<User> newMembers = LdapHelper.GetGroupMembers(entry).SelectMany(d => Replica.Users.Where(u => string.Equals(u.DistinguishedName, d, StringComparison.InvariantCultureIgnoreCase))).ToList();

            if (LdapHelper.IsDeleted(entry))
            {
                if (existing != null)
                {
                    // Delete role
                    Sender.RemoveRole(existing);
                    Replica.Groups.Remove(existing);
                }
            }
            else
            {
                if (existing != null)
                {
                    role.Id = existing.Id;

                    // Check if any attribute has changed
                    var roleXml = Sender.GetRole(role.Id);

                    if (!string.IsNullOrEmpty(roleXml))
                    {
                        bool roleChanged =
                            Role.InternalBindings.Any(
                                b =>
                                    RestHelper.GetAttributeFromReponse(roleXml, b.Value) !=
                                    (LdapHelper.GetAttributeString(entry.Attributes[b.Key], b.Key == "sAMAccountName") ?? string.Empty));
                        roleChanged |=
                            GroupBindings.Any(
                                b =>
                                    RestHelper.GetAttributeFromReponse(roleXml, b.Cms) !=
                                    (LdapHelper.GetAttributeString(entry.Attributes[b.Ldap]) ?? string.Empty));

                        if (roleChanged)
                        {
                            // Modify role
                            Sender.ModifyRole(role);
                        }
                    }
                }
                else
                {
                    // Add role
                    long? roleId = Sender.AddRole(role);

                    if (roleId != null)
                    {
                        role.Id = roleId.Value;
                        Replica.Groups.Add(role);
                    }
                }

                // Add members
                var addedMembers = newMembers.Where(m => currentMembers.All(c => c.Guid != m.Guid)).ToList();
                foreach (var member in addedMembers)
                {
                    var userroleId = Sender.AddUserToRole(member.Id, role.Id);

                    if (userroleId != null)
                    {
                        Replica.Bindings.Add(new UserRoleBinding(member.Id, role.Id) { Id = userroleId.Value });
                    }
                }

                // Remove members
                var removedMembers =
                    currentMembers.Where(m => newMembers.All(c => c.Guid != m.Guid))
                        .SelectMany(m => Replica.Bindings.Where(b => (b.RoleId == role.Id) && (b.UserId == m.Id)))
                        .ToList();
                foreach (var member in removedMembers)
                {
                    Sender.RemoveUserFromRole(member.Id);
                    Replica.Bindings.Remove(member);
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Remove role from CMS.
        /// </summary>
        /// <param name="role">Role to delete.</param>
        public void RemoveRole(Role role)
        {
            if (role != null)
            {
                try
                {
                    // Send REST request
                    Provider.MakeRequest(BaseUrl + "/rest/cms.role/" + role.Guid.ToString("D"), HttpVerb.Delete);

                    LogMessage("Role " + role.CodeName + " has been removed.");

                    if (OnSuccess != null)
                    {
                        OnSuccess();
                    }
                }
                catch (Exception ex)
                {
                    LogError("Deleting role " + role.CodeName + " failed.", ex);
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Modify existing role.
        /// </summary>
        /// <param name="role">Role to modify</param>
        public long? ModifyRole(Role role)
        {
            if (role != null)
            {
                try
                {
                    if (!RoleExists(role.Id))
                    {
                        LogMessage("Role " + role.CodeName + " was not modified because it doesn't exists.");
                    }
                    else
                    {
                        // Send REST request
                        var response = Provider.MakeRequest(BaseUrl + "/rest/cms.role/" + role.Id, HttpVerb.Put,
                            null, role.ToString());

                        var newId = Convert.ToInt64(RestHelper.GetAttributeFromReponse(response, role.IdTagName));

                        LogMessage("Role " + role.CodeName + " has been modified.");

                        if (OnSuccess != null)
                        {
                            OnSuccess();
                        }

                        return newId;
                    }
                }
                catch (Exception ex)
                {
                    LogError("Modifying role " + role.CodeName + " failed.", ex);
                }
            }

            return null;
        }