/// <summary> /// Get user information entered in UI by user. /// </summary> /// <param name="currentUser"></param> private void GetUserInformation() { currentUserProfile.FirstName = txtFirstName.Text.Trim(); currentUserProfile.MiddleName = txtMiddlename.Text; currentUserProfile.LastName = txtLastName.Text; currentUserProfile.Email = txtEmail.Text; if (!string.IsNullOrEmpty(txtSecurityQues.Text.Trim())) { currentUserProfile.SecurityQuestion = txtSecurityQues.Text.Trim(); } if (!string.IsNullOrEmpty(txtAnswer.Text.Trim())) { currentUserProfile.Answer = txtAnswer.Text; } currentUserProfile.City = txtCity.Text; currentUserProfile.State = txtState.Text; currentUserProfile.Country = txtCountry.Text; if (!string.IsNullOrEmpty(txtLoginName.Text.Trim()) && !string.IsNullOrEmpty(txtPassword.Text.Trim())) { currentUser = new ZentityUser(txtLoginName.Text.Trim(), txtPassword.Text.Trim(), currentUserProfile); } }
/// <summary> /// Creates the user. /// </summary> /// <param name="user">The user.</param> /// <param name="token">The token.</param> /// <param name="context">The context.</param> /// <returns>System.Boolean; true if successful, false otherwise.</returns> private static bool CreateUser(ZentityUser user, AuthenticatedToken token, ZentityContext context) { #region Parameter Validation if (user == null) { throw new ArgumentNullException("user"); } ValidateParameters(context, token); #endregion context.MetadataWorkspace.LoadFromAssembly(typeof(Identity).Assembly); if (!DataAccess.IsAdmin(token.IdentityName, context)) { throw new UnauthorizedAccessException(ConstantStrings.UnauthorizedAccessException); } if (context.Resources.OfType <Identity>().Where(res => res.IdentityName == user.LogOnName).Count() == 1) { throw new ArgumentException(string.Format(CultureInfo.CurrentUICulture, ConstantStrings.IdentityExists, user.LogOnName)); } if (!user.Register()) { return(false); } Identity identity = new Identity(); identity.Title = user.Profile.FirstName + " " + user.Profile.LastName; identity.IdentityName = user.LogOnName; context.AddToResources(identity); return(context.SaveChanges() == 0 ? false : true); }
/// <summary> /// Creates a new user and adds it to authentication and authorization stores. /// </summary> /// <param name="user">User object with all information.</param> /// <param name="token">Token of user who is allowed to create a user.</param> /// <returns>True if user is created successfully, else false.</returns> public static bool CreateUser(ZentityUser user, AuthenticatedToken token) { ////The CreateUser overload called below handles parameter validation using (ZentityContext context = new ZentityContext()) { return(CreateUser(user, token, context)); } }
/// <summary> /// Deletes a user from authentication and authorization stores. /// </summary> /// <param name="user">User object with all information.</param> /// <param name="token">Token of user who is allowed to delete the user.</param> /// <returns>True if user is deleted successfully, else false.</returns> public static bool DeleteUser(ZentityUser user, AuthenticatedToken token) { #region Parameter Validation ValidateToken(token); if (user == null) { throw new ArgumentNullException("user"); } if (user.LogOnName == AdminUserName || user.LogOnName == GuestUserName) { throw new ArgumentException(ConstantStrings.AdministratorOrGuestUpdateException); } #endregion using (ZentityContext context = new ZentityContext()) { return(DeleteUser(user, token, context)); } }
/// <summary> /// Deletes the user. /// </summary> /// <param name="user">The user.</param> /// <param name="token">The token.</param> /// <param name="context">The context.</param> /// <returns>System.Boolean; true if successful, false otherwise.</returns> private static bool DeleteUser(ZentityUser user, AuthenticatedToken token, ZentityContext context) { if (!DataAccess.IsAdmin(token.IdentityName, context)) { throw new UnauthorizedAccessException(ConstantStrings.UnauthorizedAccessException); } Identity identity = GetIdentity(user.LogOnName, context); if (identity == null) { throw new ArgumentException(string.Format( CultureInfo.CurrentUICulture, ConstantStrings.IdentityDoesNotExist, identity.IdentityName)); } // Remove relationships identity.RelationshipsAsObject.Load(); identity.RelationshipsAsSubject.Load(); foreach (Relationship relationship in identity.RelationshipsAsObject.Union( identity.RelationshipsAsSubject).ToList()) { context.DeleteObject(relationship); } // Remove identity context.DeleteObject(identity); if (context.SaveChanges() == 0) { return(false); } if (!user.Unregister()) { return(false); } return(true); }
/// <summary> /// Updates user information. /// </summary> /// <param name="user">User object with all information.</param> /// <param name="identity">Identity object with all information.</param> /// <param name="token">Token of user who is allowed to update the user.</param> /// <param name="connectionString">Connection string of authorization store.</param> /// <returns>True if user is updated successfully, else false.</returns> public static bool UpdateUser(ZentityUser user, Identity identity, AuthenticatedToken token, string connectionString) { #region Parameter Validation ValidateIdentity(identity); ValidateToken(token); ValidateStrings("connectionString", connectionString); if (user == null) { throw new ArgumentNullException("user"); } if (user.LogOnName == AdminUserName || user.LogOnName == GuestUserName) { throw new ArgumentException(ConstantStrings.AdministratorOrGuestUpdateException); } if (user.LogOnName != identity.IdentityName) { throw new ArgumentException(string.Format( CultureInfo.CurrentUICulture, ConstantStrings.InvalidZentityUserAndIdentityObject)); } #endregion using (ZentityContext context = new ZentityContext(connectionString)) { if (!UpdateIdentity(identity, token, context)) { return(false); } if (!UpdateZentityUser(user)) { return(false); } return(true); } }
/// <summary> /// Updates user information. /// </summary> /// <param name="user">User object with all information.</param> /// <param name="token">Token of user who is allowed to update the user.</param> /// <returns>True if user is updated successfully, else false.</returns> public static bool UpdateUser(ZentityUser user, AuthenticatedToken token) { #region Parameter Validation ValidateToken(token); if (user == null) { throw new ArgumentNullException("user"); } if (user.LogOnName == AdminUserName || user.LogOnName == GuestUserName) { throw new ArgumentException(ConstantStrings.AdministratorOrGuestUpdateException); } #endregion using (ZentityContext context = new ZentityContext()) { Identity identity = GetIdentity(user.LogOnName, context); identity.Title = user.Profile.FirstName + " " + user.Profile.LastName; return(UpdateUser(user, identity, token)); } }
/// <summary> /// Updates the zentity user. /// </summary> /// <param name="user">The user.</param> /// <returns>System.Boolean; true if successful, false otherwise.</returns> private static bool UpdateZentityUser(ZentityUser user) { return(user.UpdateProfile()); }
/// <summary> /// Initialize the server controls on page load. /// </summary> /// <param name="sender">Sender object</param> /// <param name="e">Event argument</param> protected void Page_Load(object sender, EventArgs e) { userToken = (AuthenticatedToken)Session[Constants.AuthenticationTokenKey]; if (!userToken.IsAdmin(Utility.CreateContext())) { throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, Resources.Resources.UnauthorizedAccessException, UserResourcePermissions.Read)); } id = Convert.ToString(Request.QueryString[Resources.Resources.QuerystringResourceId], CultureInfo.InvariantCulture); if (id != null) { using (ResourceDataAccess dataAccess = new ResourceDataAccess(Utility.CreateContext())) { identity = (Identity)dataAccess.GetResource(new Guid(id)); identity.Groups.Load(); loginName = identity.IdentityName; } } InitializeControls(); if (!string.IsNullOrEmpty(loginName) && !Page.IsPostBack) { ZentityUserAdmin adminObject = new ZentityUserAdmin(userToken); currentUserProfile = adminObject.GetUserProfile(loginName); txtLoginName.Text = loginName; txtLoginName.Enabled = false; passwordRow.Visible = false; reEnterPasswordRow.Visible = false; securityQuesRow.Visible = false; answerRow.Visible = false; txtFirstName.Text = currentUserProfile.FirstName; txtMiddlename.Text = currentUserProfile.MiddleName; txtLastName.Text = currentUserProfile.LastName; txtEmail.Text = currentUserProfile.Email; txtCity.Text = currentUserProfile.City; txtState.Text = currentUserProfile.State; txtCountry.Text = currentUserProfile.Country; if (currentUserProfile.AccountStatus == AccountStatus.Active.ToString()) { chkAccountStatus.Checked = true; } else if (currentUserProfile.AccountStatus == AccountStatus.InActive.ToString()) { chkAccountStatus.Checked = false; } currentUser = new ZentityUser(loginName, userToken, currentUserProfile); } else { if (!string.IsNullOrEmpty(loginName) && Page.IsPostBack) { ZentityUserAdmin adminObject = new ZentityUserAdmin(userToken); currentUserProfile = adminObject.GetUserProfile(loginName); currentUser = new ZentityUser(loginName, userToken, currentUserProfile); } else { currentUserProfile = new ZentityUserProfile(); } } if (!Page.IsPostBack) { FillUserGrid(); } UserTable.DeleteClicked += new EventHandler <ZentityGridEventArgs>(UserTable_DeleteClicked); }
/// <summary> /// Event will Save or Update the User information. /// </summary> /// <param name="sender">Sender object</param> /// <param name="e">Event argument</param> protected void btnSubmit_Click(object sender, EventArgs e) { try { bool result = false; GetUserInformation(); ZentityUserAdmin adminObject = new ZentityUserAdmin(userToken); if (!string.IsNullOrEmpty(loginName)) { currentUser = new ZentityUser(loginName, userToken, currentUserProfile); if (userInfoPanel.Enabled) { result = UserManager.UpdateUser(currentUser, userToken); } if (groupAssignment.IsEnable) { if (result) { result = adminObject.SetAccountStatus(currentUser.LogOnName, GetAccountStatus()); using (ResourceDataAccess dataAccess = new ResourceDataAccess(Utility.CreateContext())) { List <String> groupList = AddGroupsInIdentity(); List <String> allSearchList = groupAssignment.GetSearchList(); Identity identity = (Identity)dataAccess.GetResource(new Guid(id)); identity.Groups.Load(); List <string> existingGroups = identity.Groups .Select(group => group.Id.ToString()) .ToList(); foreach (string exsitingId in existingGroups) { if (!groupList.Contains(exsitingId)) { if (allSearchList.Contains(exsitingId)) { Group group = (Group)dataAccess.GetResource(new Guid(exsitingId)); dataAccess.RemoveIdentityFromGroup(identity, group, userToken); } } } foreach (string selectedId in groupList) { if (!existingGroups.Contains(selectedId)) { Group group = (Group)dataAccess.GetResource(new Guid(selectedId)); result = dataAccess.AddIdentityToGroup(group, identity, userToken); } } } } } } else { using (ResourceDataAccess dataAccess = new ResourceDataAccess(Utility.CreateContext())) { result = dataAccess.CreateUser(currentUser, userToken); if (result) { result = adminObject.SetAccountStatus(currentUser.LogOnName, GetAccountStatus()); Identity identity = UserManager.GetIdentity(currentUser.LogOnName, Utility.CreateContext()); List <String> groupList = AddGroupsInIdentity(); foreach (String groupId in groupList) { Group group = (Group)dataAccess.GetResource(new Guid(groupId)); result = dataAccess.AddIdentityToGroup(group, identity, userToken); } } } } if (!result) { if (string.IsNullOrEmpty(loginName)) { Utility.ShowMessage(lblMessage, Resources.Resources.LabelErrorRegistrationFail, true); } else { Utility.ShowMessage(lblMessage, Resources.Resources.LabelErrorUpdateUserFail, true); } } else { if (!string.IsNullOrEmpty(loginName)) { Utility.ShowMessage(lblMessage, string.Format(CultureInfo.InvariantCulture, Resources.Resources.LabelUserInfoUpdated, currentUser.LogOnName), false); } else { Utility.ShowMessage(lblMessage, string.Format(CultureInfo.InvariantCulture, Resources.Resources.LabelRegistrationCompleted, currentUser.LogOnName), false); ResetRegistrationForm(); } FillUserGrid(); } } catch (Exception ex) { Utility.ShowMessage(lblMessage, ex.Message, true); } }