private bool SaveItem() { bool success = false; try { if (!SPContext.Current.Web.UserIsSiteAdmin) { // if not site collection admin, redirect to message board lblMessageView.CssClass = "ms-error"; lblMessageView.Text = "Process Failed! <br> - You must be a site collection administrator to add an initial user to the application"; trMessage.Visible = true; } else { if (SecurityGroupMembership.SecurityGroupMembershipActiveCount("Administrator") == 0) { // if no admins are assigned // is current user in the users table? SPA.User currentUser = new SPA.User(Context.User.Identity.Name); if (currentUser.ID > 0) { // user exist, just assign admin role SecurityGroupMembership membership = new SecurityGroupMembership(); membership.UserID = currentUser.ID; membership.SecurityGroupID = 1; if (membership.Insert()) { lblMessageView.Text = "Permission Granted! <br> - Admin permissions have been granted to the existing user account"; trMessage.Visible = true; } else { lblMessageView.CssClass = "ms-error"; lblMessageView.Text = "Permission Assignment Failed! <br> - Admin permissions could not be granted to the existing user account. Check the application exceptions"; trMessage.Visible = true; } } else { // user does not exist, create user record based on information from the user profile service currentUser.UserName = lblAccountNameView.Text; currentUser.LastName = lblLastNameView.Text; currentUser.FirstName = lblFirstNameView.Text; currentUser.PreferredName = lblPreferredNameView.Text; currentUser.SPObjectGuid = lblUserProfileGuidView.Text; currentUser.UserProfileRecordID = Int32.Parse(hfUserProfileRecordID.Value); currentUser.Insert(); SecurityGroupMembership membership = new SecurityGroupMembership(); membership.UserID = currentUser.ID; membership.SecurityGroupID = 1; if (membership.Insert()) { lblMessageView.Text = "Permission Granted! <br> - Admin permissions have been granted to the new user account"; trMessage.Visible = true; } else { lblMessageView.CssClass = "ms-error"; lblMessageView.Text = "Permission Assignment Failed! <br> - Admin permissions could not be granted to the new user account. Check the application exceptions"; trMessage.Visible = true; } } } else { lblMessageView.CssClass = "ms-error"; lblMessageView.Text = "The application already has at least one assigned administrator. Review assignments by selecting permissions"; trMessage.Visible = true; } } } catch (Exception ex) { SPA.Error.WriteError(ex); if (ShowDebug) lblErrorMessage.Text = ex.ToString(); } return success; }
private bool SaveItem() { bool success = false; try { if (!IsValid) { Script("resizeModalDialog('True');"); } bool isUpdate = (IView == ItemView.Edit); item = (isUpdate) ? new SPA.User(ItemID) : new SPA.User(); item.Email = txtEmail.Text.Trim(); item.LastName = txtLastName.Text.Trim(); item.FirstName = txtFirstName.Text.Trim(); item.MiddleInitial = txtMiddleInitial.Text.Trim(); item.GenerationalQualifier = txtGenerationalQualifier.Text.Trim(); item.PreferredName = txtPreferredName.Text.Trim(); item.UserTypeID = int.Parse(ddlUserType.SelectedItem.Value); item.SeniorStaff = ckbxSeniorStaff.Checked; item.ITAdmin = ckbxITAdmin.Checked; item.SuppressTrngAlerts = ckbxSuppressTrngAlerts.Checked; item.ModifiedBy = CurrentUser.DisplayName; Transaction xAction = new Transaction(); if (!isUpdate) { item.CreatedBy = item.ModifiedBy; if (item.Insert()) { success = true; xAction.Action = string.Format("Successfully added {0} to the user catalog", item.UserName); xAction.Category = "Application Administration"; xAction.Type = Transaction.TYPE_SUCCESS; xAction.CreatedBy = item.ModifiedBy; xAction.Insert(); } else { xAction.Action = string.Format("Failed to add {0} to the user catalog", item.UserName); xAction.Category = "Application Administration"; xAction.Type = Transaction.TYPE_FAILURE; xAction.CreatedBy = item.ModifiedBy; xAction.Insert(); } } else { if (item.Update()) { success = true; xAction.Action = string.Format("Successfully update {0} in the user catalog", item.UserName); xAction.Category = "Application Administration"; xAction.Type = Transaction.TYPE_SUCCESS; xAction.CreatedBy = item.ModifiedBy; xAction.Insert(); } else { xAction.Action = string.Format("Failed to update {0} in the user catalog", item.UserName); xAction.Category = "Application Administration"; xAction.Type = Transaction.TYPE_FAILURE; xAction.CreatedBy = item.ModifiedBy; xAction.Insert(); } } ItemID = (success) ? item.ID : 0; } catch (Exception ex) { SPA.Error.WriteError(ex); if (ShowDebug) { lblErrorMessage.Text = ex.ToString(); } } return(success); }