protected void Page_Load(object sender, EventArgs e) { MessageUserControl.TryRun(() => { if (!Request.IsAuthenticated) { Response.Redirect("~/Account/Login.aspx"); } else { if (!User.IsInRole(SecurityRoles.SuperUser) && !User.IsInRole(SecurityRoles.AdminEdits)) { Response.Redirect("~/Account/Login.aspx"); } if (User.IsInRole(SecurityRoles.SuperUser)) { // if the user is a SuperUser then let the user see all users and all sites in dropdown lists SiteAddDropDownList.DataSourceID = "SiteList"; SiteAddDropDownList.DataValueField = "SiteId"; SiteAddDropDownList.DataTextField = "SiteName"; SiteCheckBoxList.Enabled = true; SiteCheckBoxList.Visible = true; SiteScrollDiv.Visible = true; UserListView.DataSourceID = "UserListViewODS"; UserListView.DataSource = null; } else if (User.IsInRole(SecurityRoles.AdminEdits)) { // if the user is an AdminEdit do not show him users with the superuser role and do not show him the admin site SiteAddDropDownList.DataSourceID = null; SiteController sitemgr = new SiteController(); List <Site> info = new List <Site>(); UserManager usermgr = new UserManager(); info.Add(sitemgr.Site_FindById(usermgr.GetUserSiteId(User.Identity.Name))); SiteAddDropDownList.DataSource = info; SiteAddDropDownList.DataTextField = "SiteName"; SiteAddDropDownList.DataValueField = "SiteId"; UserListView.DataSourceID = ""; UserListView.DataSource = usermgr.ListUser_BySearchParams("", new List <int>(new int[] { sitemgr.Site_FindById(usermgr.GetUserSiteId(User.Identity.Name)).SiteId }), new List <string>(), 3); SiteCheckBoxList.Enabled = false; SiteCheckBoxList.Visible = false; SiteScrollDiv.Visible = false; } if (!IsPostBack) { // change the site dropdown lists when the user first loads the page SiteAddDropDownList.DataBind(); LookupUsers(); } MessageUserControl.Visible = false; } }); }
protected void InsertUser_Click(object sender, CommandEventArgs e) { MessageUserControl.TryRun(() => { SiteController sitemgr = new SiteController(); // find the site that the user is being inserted in Site site = sitemgr.Site_FindById(int.Parse(SiteAddDropDownList.SelectedValue)); MessageUserControl.Visible = true; if (string.IsNullOrWhiteSpace(FirstNameTextBox.Text)) { throw new Exception("First name is a required field, please enter a valid first name value"); } else if (string.IsNullOrWhiteSpace(LastNameTextBox.Text)) { throw new Exception("Last name is a required field, please enter a valid last name value"); } else if (string.IsNullOrWhiteSpace(RequestedPasswordLabel.Text)) { throw new Exception("Password is a required field, please enter a valid password value"); } else if (string.IsNullOrWhiteSpace(RoleMemberships.SelectedValue)) { throw new Exception("Role is required for every employee please enter a valid role for before inserting the employee"); } else if (string.IsNullOrWhiteSpace(UserNameLabel.Text)) { throw new Exception("Please assign the user a username"); } else if (site.Disabled) { // refresh the site dropdown list without disabled sites if the user attempted to insert a user into a disabled site SiteAddDropDownList.DataBind(); throw new Exception("Please select a site which is not deactivated"); } else { UserProfile user = new UserProfile(); Utility utility = new Utility(); user.UserName = UserNameLabel.Text; user.FirstName = FirstNameTextBox.Text; user.LastName = LastNameTextBox.Text; user.SiteId = int.Parse(SiteAddDropDownList.SelectedValue); user.RequestedPassword = RequestedPasswordLabel.Text; var roleList = new List <string>(); roleList.Add(RoleMemberships.SelectedValue); user.RoleMemberships = roleList; user.Active = (disabledCheckBox.Checked); utility.checkValidString(user.UserName); utility.checkValidString(user.FirstName); utility.checkValidString(user.LastName); utility.checkValidString(user.RequestedPassword); UserManager sysmgr = new UserManager(); sysmgr.AddUser(user); if (User.IsInRole(SecurityRoles.SuperUser)) { // if the user is a SuperUser then let the user see all users and all sites in dropdown lists SiteAddDropDownList.DataSourceID = "SiteList"; SiteAddDropDownList.DataValueField = "SiteId"; SiteAddDropDownList.DataTextField = "SiteName"; SiteCheckBoxList.Enabled = true; SiteCheckBoxList.Visible = true; SiteScrollDiv.Visible = true; UserListView.DataSourceID = "UserListViewODS"; UserListView.DataSource = null; } else if (User.IsInRole(SecurityRoles.AdminEdits)) { // if the user is an AdminEdit do not show him users with the superuser role and do not show him the admin site SiteAddDropDownList.DataSourceID = null; List <Site> info = new List <Site>(); info.Add(sitemgr.Site_FindById(sysmgr.GetUserSiteId(User.Identity.Name))); SiteAddDropDownList.DataSource = info; SiteAddDropDownList.DataTextField = "SiteName"; SiteAddDropDownList.DataValueField = "SiteId"; UserListView.DataSourceID = ""; UserListView.DataSource = sysmgr.ListUser_BySearchParams("", new List <int>(new int[] { sitemgr.Site_FindById(sysmgr.GetUserSiteId(User.Identity.Name)).SiteId }), new List <string>(), 3); SiteCheckBoxList.Enabled = false; SiteCheckBoxList.Visible = false; SiteScrollDiv.Visible = false; } UserListView.DataBind(); LookupUsers(); CancelButton_Command(sender, e); MessageUserControl.Visible = true; } }, "Success", "New user has been added."); }