Example #1
0
        public void btnFind_Click(object sender, System.EventArgs e)
        {
            try
            {
                Nav1.Feedback.Text = String.Empty;
                UserM user = null;
                if (txtUserName.Text != null && !txtUserName.Text.Trim().Equals(String.Empty))
                {
                    user = UserM.LoadByUserName(txtUserName.Text.Trim());
                }
                else if (txtUniversityIdentifier.Text != null && !txtUniversityIdentifier.Text.Trim().Equals(String.Empty))
                {
                    user = UserM.LoadByUniversityID(txtUniversityIdentifier.Text.Trim());
                }
                else if (txtEMailAddress != null && !txtEMailAddress.Text.Trim().Equals(String.Empty))
                {
                    user = UserM.LoadByEmail(txtEMailAddress.Text.Trim());
                }

                if (user != null && user.IsValid)
                {
                    //Populate result
                    Response.Redirect("AddEditUser.aspx?CourseID=" + courseId + "&UserID=" + user.UserID, false);
                }
                else
                {
                    Nav1.Feedback.Text = SharedSupport.GetLocalizedString("AddEditUser_NoUserRecord") + Server.HtmlEncode(txtUserName.Text.ToString());
                    clearFields();
                }
            }
            catch (Exception ex)
            {
                Nav1.Feedback.Text = ex.Message.ToString();
                btnUpdate.Visible  = false;
            }
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"> </param>
        /// <param name="e"> </param>
        public void btnUpdate_Click(object sender, System.EventArgs e)
        {
            try
            {
                //reset error handling label
                Nav1.Feedback.Text = String.Empty;
                checkErrorCases();

                UserM user = null;
                //Save Updated or New User - check for UserID on query string
                if (userId != 0)
                {
                    if (!SharedSupport.SecurityIsAllowed(courseId, SecurityAction.USER_EDIT))
                    {
                        throw new Exception(SharedSupport.GetLocalizedString("Global_Unauthorized"));
                    }

                    //Update
                    user = UserM.Load(userId);

                    if (user.IsValid)
                    {
                        //Save updated user
                        user.EmailAddress      = txtEMailAddress.Text.ToString();
                        user.FirstName         = txtFirstName.Text.ToString();
                        user.LastName          = txtLastName.Text.ToString();
                        user.LastUpdatedDate   = DateTime.Now;
                        user.LastUpdatedUserID = SharedSupport.GetUserIdentity();
                        user.MiddleName        = txtMiddleName.Text.ToString();
                        user.UniversityID      = txtUniversityIdentifier.Text.ToString();
                        user.UserName          = txtUserName.Text.ToString();

                        user.Update();

                        if (user.IsInCourse(courseId))
                        {
                            if (SharedSupport.SecurityIsAllowed(courseId, SecurityAction.SECURITY_EDIT))
                            {
                                int   roleid           = Convert.ToInt32(UserRolesList.SelectedItem.Value);
                                RoleM currentUsersRole = RoleM.GetUsersRoleInCourse(SharedSupport.GetUserIdentity(), courseId);

                                // The lower role => greater permissions
                                if ((currentUsersRole.ID == (int)PermissionsID.Admin) || (currentUsersRole.ID < roleid))
                                {
                                    user.SetRoleInCourse(courseId, roleid);
                                }
                                else
                                {
                                    throw new Exception(SharedSupport.GetLocalizedString("AddEditUser_ErrorRolePermissionDenied"));
                                }
                            }
                        }
                        else
                        {
                            // Add user to Course
                            PermissionsID permission = PermissionsID.Student;
                            if (SharedSupport.SecurityIsAllowed(courseId, SecurityAction.SECURITY_EDIT))
                            {
                                int   roleid           = Convert.ToInt32(UserRolesList.SelectedItem.Value);
                                RoleM currentUsersRole = RoleM.GetUsersRoleInCourse(SharedSupport.GetUserIdentity(), courseId);

                                // The lower role => greater permissions
                                // Note: Cannot change the permission of someone at your level.
                                if ((currentUsersRole.ID == (int)PermissionsID.Admin) || (currentUsersRole.ID < roleid))
                                {
                                    permission = (PermissionsID)roleid;
                                    user.AddToCourse(courseId, permission);
                                }
                                else
                                {
                                    throw new Exception(SharedSupport.GetLocalizedString("AddEditUser_ErrorRolePermissionDenied"));
                                }
                            }
                        }
                        btnUpdate.Text     = SharedSupport.GetLocalizedString("AddEditUser_Update");
                        Nav1.Feedback.Text = SharedSupport.GetLocalizedString("AddEditUser_UserUpdated");                          //"User has been Updated.";
                    }
                    else
                    {
                        throw new Exception(NO_USER_FOR_USERID_ERROR);
                    }
                }
                else
                {
                    if (!SharedSupport.SecurityIsAllowed(courseId, SecurityAction.USER_ADD))
                    {
                        throw new Exception(SharedSupport.GetLocalizedString("Global_Unauthorized"));
                    }

                    //Insert
                    user = new UserM();

                    user.EmailAddress      = txtEMailAddress.Text.ToString();
                    user.FirstName         = txtFirstName.Text.ToString();
                    user.LastName          = txtLastName.Text.ToString();
                    user.LastUpdatedDate   = DateTime.Now;
                    user.LastUpdatedUserID = SharedSupport.GetUserIdentity();
                    user.MiddleName        = txtMiddleName.Text.ToString();
                    user.UniversityID      = txtUniversityIdentifier.Text.ToString();
                    user.UserName          = txtUserName.Text.ToString();
                    user.ChangedPassword   = false;

                    // Does the user already exist?
                    UserM userByName = UserM.LoadByUserName(user.UserName);
                    if (!userByName.IsValid)
                    {
                        userId = user.Create();

                        btnUpdate.Text     = SharedSupport.GetLocalizedString("AddEditUser_Update");
                        Nav1.Feedback.Text = SharedSupport.GetLocalizedString("AddEditUser_UserInserted");                          //"User has been inserted.";

                        PermissionsID permission = PermissionsID.Student;
                        if (SharedSupport.SecurityIsAllowed(courseId, SecurityAction.SECURITY_EDIT))
                        {
                            int   roleid           = Convert.ToInt32(UserRolesList.SelectedItem.Value);
                            RoleM currentUsersRole = RoleM.GetUsersRoleInCourse(SharedSupport.GetUserIdentity(), courseId);

                            // The lower role = greater permissions
                            // Note: Can't change permissions of someone equal in level to you.
                            if ((currentUsersRole.ID == (int)PermissionsID.Admin) || (currentUsersRole.ID < roleid))
                            {
                                permission = (PermissionsID)roleid;
                            }
                            else
                            {
                                throw new Exception(SharedSupport.GetLocalizedString("AddEditUser_ErrorRolePermissionDenied"));
                            }
                        }
                        user.AddToCourse(courseId, permission);
                    }
                    else
                    {
                        throw new Exception(SharedSupport.GetLocalizedString("User_UserNameMustBeUnique"));
                    }
                }

                Response.Redirect("Users.aspx?UserID=" + userId.ToString() + "&" + Request.QueryString.ToString(), false);
            }
            catch (Exception ex)
            {
                Nav1.Feedback.Text = ex.Message.ToString();
            }
        }
        private void btnImportRecords_Click(object sender, System.EventArgs e)
        {
            try
            {
                Nav1.Feedback.Text = String.Empty;
                //Validate delimiting character not blank
                if (delimitingCharacter == String.Empty)
                {
                    Nav1.Feedback.Text = SharedSupport.GetLocalizedString("AdminImport_ChooseDelimitingChar");
                    return;
                }

                System.Data.DataSet dsuser = SharedSupport.ParseDelimitedFile(uploadedFilePath, delimitingCharacter);
                //Grab the column order from the drop downs and put into string array

                int[] columns = new int[6];

                if (!cboLastName.SelectedIndex.Equals(0) && !cboLastName.SelectedIndex.Equals(-1))
                {
                    if (!checkMultipleColumn(columns, cboLastName.SelectedIndex))
                    {
                        columns[0] = cboLastName.SelectedIndex;
                    }
                    else
                    {
                        throw new ApplicationException(SharedSupport.GetLocalizedString("AdminImport_ColumnOnce"));
                    }
                }
                else
                {
                    //throw required field error.
                    throw new ApplicationException(SharedSupport.GetLocalizedString("AdminImport_MissingLastName"));
                }
                if (!cboFirstName.SelectedIndex.Equals(0) && !cboFirstName.SelectedIndex.Equals(-1))
                {
                    if (!checkMultipleColumn(columns, cboFirstName.SelectedIndex))
                    {
                        columns[1] = cboFirstName.SelectedIndex;
                    }
                    else
                    {
                        throw new ApplicationException(SharedSupport.GetLocalizedString("AdminImport_ColumnOnce"));
                    }
                }
                else
                {
                    //throw required field error.
                    throw new ApplicationException(SharedSupport.GetLocalizedString("AdminImport_MissingFirstName"));
                }
                if (!cboMiddleName.SelectedIndex.Equals(0) && !cboMiddleName.SelectedIndex.Equals(-1))
                {
                    if (!checkMultipleColumn(columns, cboMiddleName.SelectedIndex))
                    {
                        columns[2] = cboMiddleName.SelectedIndex;
                    }
                    else
                    {
                        throw new ApplicationException(SharedSupport.GetLocalizedString("AdminImport_ColumnOnce"));
                    }
                }
                else
                {
                    columns[2] = -1;
                }
                if (!cboEmailAddress.SelectedIndex.Equals(0) && !cboEmailAddress.SelectedIndex.Equals(-1))
                {
                    if (!checkMultipleColumn(columns, cboEmailAddress.SelectedIndex))
                    {
                        columns[3] = cboEmailAddress.SelectedIndex;
                    }
                    else
                    {
                        throw new ApplicationException(SharedSupport.GetLocalizedString("AdminImport_ColumnOnce"));
                    }
                }
                else
                {
                    //throw required field error.
                    throw new ApplicationException(SharedSupport.GetLocalizedString("AdminImport_MissingEmail"));
                }
                if (!cboUniversityID.SelectedIndex.Equals(0) && !cboUniversityID.SelectedIndex.Equals(-1))
                {
                    if (!checkMultipleColumn(columns, cboUniversityID.SelectedIndex))
                    {
                        columns[4] = cboUniversityID.SelectedIndex;
                    }
                    else
                    {
                        throw new ApplicationException(SharedSupport.GetLocalizedString("AdminImport_ColumnOnce"));
                    }
                }
                else
                {
                    //throw required field error.
                    throw new ApplicationException(SharedSupport.GetLocalizedString("AdminImport_MissingID"));
                }
                if (!cboUserName.SelectedIndex.Equals(0) && !cboUserName.SelectedIndex.Equals(-1))
                {
                    if (!checkMultipleColumn(columns, cboUserName.SelectedIndex))
                    {
                        columns[5] = cboUserName.SelectedIndex;
                    }
                    else
                    {
                        throw new ApplicationException(SharedSupport.GetLocalizedString("AdminImport_ColumnOnce"));
                    }
                }
                else
                {
                    //throw required field error.
                    throw new ApplicationException(SharedSupport.GetLocalizedString("AdminImport_MissingUserName"));
                }

                //Make sure that each column is only choosen once.
                //Grab the userId from the cookie
                int    UserID        = SharedSupport.GetUserIdentity();
                int    importErrors  = 0;
                int    importSuccess = 0;
                string importID      = System.Guid.NewGuid().ToString();
                for (int i = 0; i < dsuser.Tables[0].Rows.Count; i++)
                {
                    try
                    {
                        string userName = dsuser.Tables[0].Rows[i][columns[5] - 1].ToString();
                        // Does the user already exist?
                        UserM userByName = UserM.LoadByUserName(userName);
                        if (userByName.IsValid)
                        {
                            throw new Exception(SharedSupport.GetLocalizedString("User_UserNameMustBeUnique"));
                        }
                        UserM user = new UserM();
                        user.LastName  = dsuser.Tables[0].Rows[i][columns[0] - 1].ToString();
                        user.FirstName = dsuser.Tables[0].Rows[i][columns[1] - 1].ToString();
                        if (!columns[2].Equals(-1))
                        {
                            user.MiddleName = dsuser.Tables[0].Rows[i][columns[2] - 1].ToString();
                        }
                        user.EmailAddress      = dsuser.Tables[0].Rows[i][columns[3] - 1].ToString();
                        user.UniversityID      = dsuser.Tables[0].Rows[i][columns[4] - 1].ToString();
                        user.UserName          = userName;
                        user.LastUpdatedUserID = UserID;
                        user.LastUpdatedDate   = DateTime.Now;
                        user.ChangedPassword   = false;
                        // create but do not mail out password.
                        user.Create(false);
                        if (!user.IsInCourse(courseId))
                        {
                            user.ImportToCourse(courseId, importID);
                        }
                        importSuccess++;
                    }
                    catch
                    {
                        importErrors++;
                    }
                }

                //Delete imported file
                System.IO.File.Delete(uploadedFilePath);
                //Redirect to Results page.
                Response.Redirect("Results.aspx?CourseID=" + courseId.ToString() + "&ImportID=" + importID + "&Success=" + importSuccess + "&Errors=" + importErrors + "&Expected=" + dsuser.Tables[0].Rows.Count, false);
            }
            catch (Exception ex)
            {
                Nav1.Feedback.Text = SharedSupport.GetLocalizedString("AdminImport_GenericError");
            }
        }