/// <summary> /// Create the user with the information we've collected. /// </summary> /// <param name="email">The identity or login name of the user.</param> /// <param name="name">The user's real name name.</param> /// <param name="description">A description of the user.</param> /// <param name="groupId">The user's initial group.</param> /// <param name="organization">The organization the user belongs to.</param> private void Create(string email, string name, string description, Guid groupId, Guid organization) { try { AdminSupportClient client = new AdminSupportClient(Guardian.Properties.Settings.Default.AdminSupportEndpoint); AdminSupportReference.User record = new AdminSupportReference.User(); record.FullName = name; record.EmailAddress = email; record.Description = description; record.LookupId = email; record.Organization = organization; record.GroupId = groupId; MethodResponseguid response = client.CreateUser(record, null); client.Close(); if (!response.IsSuccessful) { this.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(this, String.Format(Properties.Resources.CreateUserFailed, name), this.Title))); } } catch (Exception exception) { // Any issues trying to communicate to the server are logged. EventLog.Error("{0}, {1}", exception.Message, exception.StackTrace); this.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(this, String.Format(Properties.Resources.CreateUserFailed, name), this.Title))); } }
/// <summary> /// Populate a user record with information about this user. /// </summary> /// <param name="record">The record to populate.</param> protected void PopulateRecord(AdminSupportReference.User record) { record.Description = this.Description; record.EmailAddress = this.EmailAddress; record.FullName = this.Name; record.LookupId = this.IdentityName; record.Organization = this.TenantId; if (this.DefaultGroup != null) { record.GroupId = this.DefaultGroup.EntityId; } record.UserId = this.UserId; }
/// <summary> /// Set the user's password to the new password. /// </summary> /// <param name="user">The user to change.</param> /// <param name="oldPassword">The current password.</param> /// <param name="password">The new password.</param> private void ResetPassword(User user, string oldPassword, string password) { try { AdminSupportClient adminSupportClient = new AdminSupportClient(Guardian.Properties.Settings.Default.AdminSupportEndpoint); AdminSupportReference.User userRecord = new AdminSupportReference.User(); MethodResponseErrorCode response = null; DataModel.IsReading = false; if (user.UserId == UserContext.Instance.UserId) { response = adminSupportClient.ChangePassword(oldPassword, password); if (response.IsSuccessful) { ChannelStatus.LoginEvent.Set(); ChannelStatus.IsPrompted = false; ChannelStatus.Secret = password; ChannelStatus.LogggedInEvent.Set(); } } else { response = adminSupportClient.ResetPassword(user.IdentityName, password); } if (!response.IsSuccessful) { GuardianObject.ThrowErrorInfo(response.Errors[0]); } adminSupportClient.Close(); } catch (FaultException <ArgumentFault> ) { this.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(this, String.Format(Properties.Resources.ResetPasswordFailedPoorComplexity, user), this.Title))); } catch (SecurityAccessDeniedException) { this.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(this, String.Format(Properties.Resources.UserNotFound, user), this.Title))); } catch (FaultException <RecordNotFoundFault> ) { this.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(this, String.Format(Properties.Resources.ResetPasswordFailedPermissionDenied, user), this.Title))); } catch (Exception exception) { // Any issues trying to communicate to the server are logged. EventLog.Error("{0}, {1}", exception.Message, exception.StackTrace); this.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(this, String.Format(Properties.Resources.ResetPasswordFailed, user.Name), this.Title))); } finally { DataModel.IsReading = true; } }
/// <summary> /// Commit any changes to this user to the server. /// </summary> public override void Commit() { AdminSupportClient client = new AdminSupportClient(Guardian.Properties.Settings.Default.AdminSupportEndpoint); AdminSupportReference.User user = new AdminSupportReference.User(); MethodResponseErrorCode response; this.PopulateRecord(user); if (this.Deleted) { response = client.DeleteUserAccount(user.LookupId); if (this.GetFirstErrorCode(response) == ErrorCode.RecordNotFound) { throw new UserNotFoundException(this, "User not found"); } } else { response = client.UpdateUser(new AdminSupportReference.User[] { user }); if (this.GetFirstErrorCode(response) == ErrorCode.RecordNotFound) { throw new UserNotFoundException(this, "User not found"); } if (response.IsSuccessful) { if (this.AccountDisabled) { response = client.DisableUserAccount(this.IdentityName); } } if (response.IsSuccessful) { lock (DataModel.SyncRoot) { List <Group> newGroups = this.Groups.ToList(); List <Guid> add = new List <Guid>(); List <Guid> del = new List <Guid>(); GroupUsersRow[] oldGroups = DataModel.User.UserKey.Find(this.UserId).GetGroupUsersRows(); ErrorCode firstError; foreach (GroupUsersRow groupUsersRow in oldGroups) { Group group = newGroups.FirstOrDefault(g => g.GroupId == groupUsersRow.GroupId); if (group == null) { del.Add(groupUsersRow.GroupId); } else { if (group.Deleted) { del.Add(group.GroupId); } newGroups.Remove(group); } } foreach (Group group in newGroups) { response = client.AddUserToGroup(this.IdentityName, group.GroupId, this.TenantId); firstError = this.GetFirstErrorCode(response); if (firstError == ErrorCode.RecordNotFound) { throw new GroupNotFoundException(this.DefaultGroup, "Group not found"); } else if (firstError != ErrorCode.Success) { break; } } foreach (Guid group in del) { response = client.RemoveUserFromGroup(this.IdentityName, group); firstError = this.GetFirstErrorCode(response); if (firstError != ErrorCode.RecordNotFound && firstError != ErrorCode.Success) { break; } } } } } if (!response.IsSuccessful) { GuardianObject.ThrowErrorInfo(response.Errors[0]); } client.Close(); this.Modified = false; }