private void FillAppSecurityGroupMembers(int SecurityGroupID) { try { lbxAppGroupMembers.Items.Clear(); DataSet ds = SecurityGroupMembership.Items(SecurityGroupID); DataView dv = new DataView(ds.Tables[0]); dv.Sort = "DisplayName ASC"; lbxAppGroupMembers.DataSource = dv; lbxAppGroupMembers.DataTextField = "DisplayName"; lbxAppGroupMembers.DataValueField = "UserID"; lbxAppGroupMembers.DataBind(); if (lbxAppGroupMembers.Items.Count > 0) { btnRemove.Enabled = true; } else { btnRemove.Enabled = false; } } catch (Exception ex) { SPA.Error.WriteError(ex); if (ShowDebug) { lblErrorMessage.Text = ex.ToString(); } } }
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; }
protected void btnAdd_Click(object sender, EventArgs e) { try { if (hfViewMode.Value == "app") { // get role name associated with selected security group SecurityGroup Group = new SecurityGroup(Int32.Parse(lbxAppGroups.SelectedValue)); Role role = new Role(Group.RoleID); // evaluate if user may administor members of security group based on role if (CurrentUser.InRole(role.Name)) { foreach (ListItem li in lbxUserLookup.Items) { if (li.Selected) { // add individual membership SecurityGroupMembership membership = new SecurityGroupMembership(); membership.SecurityGroupID = Int32.Parse(lbxAppGroups.SelectedValue); membership.UserID = int.Parse(li.Value); membership.CreatedBy = CurrentUser.DisplayName; membership.ModifiedBy = CurrentUser.DisplayName; if (!membership.Insert()) { Transaction xAction = new Transaction(); xAction.Action = string.Format("Failed to add {0} to security group {1}", li.Text, lbxAppGroups.SelectedItem.Text); xAction.Category = "Application Administration"; xAction.Type = Transaction.TYPE_FAILURE; xAction.CreatedBy = CurrentUser.DisplayName; xAction.Insert(); } else { Transaction xAction = new Transaction(); xAction.Action = string.Format("Successfully added {0} to security group {1}", li.Text, lbxAppGroups.SelectedItem.Text); xAction.Category = "Application Administration"; xAction.Type = Transaction.TYPE_SUCCESS; xAction.CreatedBy = CurrentUser.DisplayName; xAction.Insert(); Action.Write(string.Format("Assigned {0} to the application security group {1}", li.Text, lbxAppGroups.SelectedItem.Text), CurrentUser.DisplayName); } } } FillAppSecurityGroupMembers(Int32.Parse(lbxAppGroups.SelectedValue)); } else { ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('You do not have permission to add members to this security group!');", true); } } else { // get role name associated with selected security group OrgSecurityGroup Group = new OrgSecurityGroup(Int32.Parse(lbxOrgGroups.SelectedValue)); Role role = new Role(Group.RoleID); // evaluate if user may administor members of security group based on role if (CurrentUser.InRole(role.Name, int.Parse(ddlOrganizations.SelectedValue))) { foreach (ListItem li in lbxUserLookup.Items) { if (li.Selected) { // add individual membership OrgSecurityGroupMembership membership = new OrgSecurityGroupMembership(); membership.OrgSecurityGroupID = Int32.Parse(lbxOrgGroups.SelectedValue); membership.OrgGroupID = int.Parse(ddlOrganizations.SelectedValue); membership.UserID = int.Parse(li.Value); membership.CreatedBy = CurrentUser.DisplayName; membership.ModifiedBy = CurrentUser.DisplayName; if (!membership.Insert()) { Transaction xAction = new Transaction(); xAction.Action = string.Format("Failed to add {0} to org security group {1}", li.Text, lbxOrgGroups.SelectedItem.Text); xAction.Category = "Application Administration"; xAction.Type = Transaction.TYPE_FAILURE; xAction.CreatedBy = CurrentUser.DisplayName; xAction.Insert(); } else { Transaction xAction = new Transaction(); xAction.Action = string.Format("Successfully added {0} to org security group {1}", li.Text, lbxOrgGroups.SelectedItem.Text); xAction.Category = "Application Administration"; xAction.Type = Transaction.TYPE_SUCCESS; xAction.CreatedBy = CurrentUser.DisplayName; xAction.Insert(); Action.Write(string.Format("Assigned {0} to the {1} application security group {2}", li.Text, ddlOrganizations.SelectedItem.Text, lbxOrgGroups.SelectedItem.Text), CurrentUser.DisplayName); } } } FillOrgSecurityGroupMembers(Int32.Parse(lbxOrgGroups.SelectedValue), int.Parse(ddlOrganizations.SelectedValue)); } else { ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('You do not have permission to add members to this security group!');", true); } } } catch (Exception ex) { SPA.Error.WriteError(ex); if (ShowDebug) { lblErrorMessage.Text = ex.ToString(); } } }