Exemplo n.º 1
0
        /// <summary>
        /// Remove user from CMS.
        /// </summary>
        /// <param name="user">User to delete.</param>
        public void RemoveUser(User user)
        {
            if (user != null)
            {
                try
                {
                    // Send REST request
                    Provider.MakeRequest(BaseUrl + "/rest/cms.user/" + user.Guid.ToString("D"), HttpVerb.Delete);

                    LogMessage("User " + user.CodeName + " has been removed.");

                    if (OnSuccess != null)
                    {
                        OnSuccess();
                    }
                }
                catch (Exception ex)
                {
                    LogError("Removing user " + user.CodeName + " failed.", ex);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Handle single user.
        /// </summary>
        /// <param name="entry">User LDAP object.</param>
        private void HandleUser(SearchResultEntry entry)
        {
            // Create CMS object from LDAP object
            var user = new User(
                LdapHelper.GetObjectGuid(entry),
                LdapHelper.GetAttributeString(entry.Attributes["name"], true),
                LdapHelper.IsUserEnabled(entry),
                UserBindings.Select(k => new KeyValuePair<string, string>(k.Cms, LdapHelper.GetAttributeString(entry.Attributes[k.Ldap]))).ToList());

            // Find existing object in LDAP replica
            var existing = Replica.Users.FirstOrDefault(u => u.Guid == user.Guid);

            if (LdapHelper.IsDeleted(entry))
            {
                if (existing != null)
                {
                    // Remove user
                    Sender.RemoveUser(existing);
                    Replica.Users.Remove(existing);
                }
            }
            else if (existing != null)
            {
                // Check if any attribute has changed
                var userXml = Sender.GetUser(user.Guid);

                if (!string.IsNullOrEmpty(userXml))
                {
                    bool userChanged =
                        User.InternalBindings.Any(
                            b =>
                                RestHelper.GetAttributeFromReponse(userXml, b.Value) !=
                                ((b.Key == "userAccountControl" ? LdapHelper.IsUserEnabled(entry).ToString().ToLowerInvariant() : LdapHelper.GetAttributeString(entry.Attributes[b.Key], b.Key == "name")) ?? string.Empty));
                    userChanged |=
                        UserBindings.Any(
                            b =>
                                RestHelper.GetAttributeFromReponse(userXml, b.Cms) !=
                                (LdapHelper.GetAttributeString(entry.Attributes[b.Ldap]) ?? string.Empty));

                    if (userChanged)
                    {
                        // Modify user
                        Sender.ModifyUser(user);
                    }
                }
            }
            else
            {
                // Add user
                long? userId = Sender.AddUser(user);

                if (userId != null)
                {
                    user.Id = userId.Value;
                    user.DistinguishedName = entry.DistinguishedName;
                    Replica.Users.Add(user);
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Modify existing user in CMS.
        /// </summary>
        /// <param name="user">Modified user</param>
        /// <returns>ID of modified user.</returns>
        public long? ModifyUser(User user)
        {
            if (user != null)
            {
                try
                {
                    if (!UserExists(user.Guid))
                    {
                        LogMessage("User " + user.CodeName + " was not modified because it doesn't exists.");
                    }
                    else
                    {
                        // Send REST request
                        var response = Provider.MakeRequest(BaseUrl + "/rest/cms.user/" + user.Guid, HttpVerb.Put,
                            null, user.ToString());

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

                        LogMessage("User " + user.CodeName + " has been modified.");

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

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

            return null;
        }