Exemple #1
0
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Page.Validate();
        if (Page.IsValid)
        {
            try
            {
                //creating user

                if (ddlARC.SelectedValue == "-1")
                {
                    string script = "alertify.alert('" + ltrSelectARC.Text + "');";
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", script, true);
                    MaintainScrollPositionOnPostBack = false;
                    return;
                }
                var checkedroles = (from ListItem item in Chkboxroles.Items where item.Selected select item.Value).ToList();
                if (!checkedroles.Any())
                {
                    string script = "alertify.alert('" + ltrSelectRole.Text + "');";
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", script, true);
                    MaintainScrollPositionOnPostBack = false;
                    return;
                }
                string username = "";
                if (Session[enumSessions.UserIdToUpdate.ToString()] == null)
                {
                    txtuname.Enabled = true;
                    if (!string.IsNullOrEmpty(txtpwd.Text.ToString().Trim()) && !string.IsNullOrEmpty(Txtuemail.Text.ToString().Trim()) && !string.IsNullOrEmpty(txtuname.Text.ToString().Trim()))
                    {
                        username = txtuname.Text.ToString().Trim();
                        string password = txtpwd.Text.ToString().Trim();
                        string Emailid  = Txtuemail.Text.ToString().Trim();
                        string question = ddlSecurityQuestion.SelectedValue;
                        string answer   = txtAnswer.Text.ToString().Trim();
                        MembershipCreateStatus res;
                        MembershipUser         usr = Membership.CreateUser(username, password, Emailid, question, answer, ChkBoxIsapproved.Checked, out res);
                        if (usr == null)
                        {
                            string script = "alertify.alert('" + res.ToString() + "');";
                            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", script, true);
                            return;
                        }
                        else
                        {
                            Session[enumSessions.UserIdToUpdate.ToString()] = new Guid(usr.ProviderUserKey.ToString());
                            string script = "alertify.alert('User " + txtuname.Text + " created successfully.');";
                            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", script, true);
                            MaintainScrollPositionOnPostBack = false;
                        }
                    }
                }
                //updating user
                else
                {
                    if (!string.IsNullOrEmpty(Txtuemail.Text.ToString().Trim()) && !string.IsNullOrEmpty(txtuname.Text.ToString().Trim()))
                    {
                        txtuname.Enabled = false;
                        username         = txtuname.Text.ToString().Trim();
                        string         password = txtpwd.Text.ToString().Trim();
                        string         Emailid  = Txtuemail.Text.ToString().Trim();
                        string         question = ddlSecurityQuestion.SelectedValue;
                        string         answer   = txtAnswer.Text.ToString().Trim();
                        MembershipUser user;
                        user = Membership.GetUser(new Guid(Session[enumSessions.UserIdToUpdate.ToString()].ToString()));
                        db   = new LinqToSqlDataContext();
                        if (ChkBoxIsBlocked.Checked == false)
                        {
                            user.UnlockUser();
                        }
                        var usrDtls = db.USP_GetUserDetailsByUserId(Session[enumSessions.UserIdToUpdate.ToString()].ToString()).FirstOrDefault();
                        // string cur_pwd = user.GetPassword(usrDtls.PasswordAnswer);
                        // user.ChangePasswordQuestionAndAnswer(cur_pwd, question, answer);
                        if (!string.IsNullOrEmpty(txtpwd.Text.ToString()))
                        {
                            user.ChangePassword(Membership.Provider.ResetPassword(username, usrDtls.PasswordAnswer), txtpwd.Text);
                            // user.ChangePassword(cur_pwd, txtpwd.Text.ToString().Trim());
                        }

                        user.Email = Emailid.Trim();

                        Boolean approved = true;
                        if (ChkBoxIsapproved.Checked)
                        {
                            approved = true;
                        }
                        else
                        {
                            approved = false;
                        }


                        user.IsApproved = approved;
                        Membership.UpdateUser(user);

                        //deleting old existing roles of this user
                        string[] adminroles = (from a in db.ApplicationSettings
                                               where a.KeyName == enumApplicationSetting.WebsiteAdminRoles.ToString()
                                               select a.KeyValue).SingleOrDefault().Split(',');
                        var Rls = Roles.GetAllRoles().Except(adminroles).ToList();

                        foreach (string Urole in Rls)
                        {
                            if (Roles.IsUserInRole(txtuname.Text.ToString(), Urole))
                            {
                                Roles.RemoveUserFromRole(txtuname.Text.ToString(), Urole);
                            }
                        }

                        //deleting old existing arcs of this user

                        db = new LinqToSqlDataContext();
                        var delarc = db.ARC_User_Maps.Where(item => item.UserId == new Guid(Session[enumSessions.UserIdToUpdate.ToString()].ToString()));
                        db.ARC_User_Maps.DeleteAllOnSubmit(delarc);
                        db.SubmitChanges();

                        string script = "alertify.alert('User " + txtuname.Text + " updated successfully.');";
                        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", script, true);
                        MaintainScrollPositionOnPostBack = false;
                    }
                }

                string roleslist = string.Empty;
                //inserting checked roles
                for (int i = 0; i <= Chkboxroles.Items.Count - 1; i++)
                {
                    if (Chkboxroles.Items[i].Selected == true)
                    {
                        Roles.AddUserToRole(txtuname.Text.ToString(), Chkboxroles.Items[i].Value.ToString());
                        roleslist += Chkboxroles.Items[i].Value.ToString() + ",";
                    }
                }


                //inserting checked arcs of this user

                ARC_User_Map acm;
                if (ddlARC.SelectedValue != "-1" && ddlARC.SelectedValue != null)
                {
                    db         = new LinqToSqlDataContext();
                    acm        = new ARC_User_Map();
                    acm.UserId = new Guid(Session[enumSessions.UserIdToUpdate.ToString()].ToString());
                    acm.ARCId  = Convert.ToInt32(ddlARC.SelectedValue);
                    db.ARC_User_Maps.InsertOnSubmit(acm);
                    db.SubmitChanges();
                    int orderId = (from o in db.Orders
                                   where o.UserId == acm.UserId && o.ARCId != acm.ARCId && o.OrderStatusId == 1
                                   select o.OrderId).SingleOrDefault();
                    if (orderId > 0)
                    {
                        db.USP_DeleteOrderwithDetails(orderId);
                    }
                }


                pnluserdetails.Visible = false;
                pnluserlist.Visible    = true;

                Audit audit = new Audit();
                audit.UserName  = Session[enumSessions.User_Name.ToString()].ToString();
                audit.ChangeID  = Convert.ToInt32(enumAudit.Manage_User);
                audit.CreatedOn = DateTime.Now;
                audit.Notes     = "UserName: "******", Email: " + Txtuemail.Text + ", ARC: " + ddlARC.SelectedItem + ", IsApproved: " + ChkBoxIsapproved.Checked +
                                  ", IsBlocked:" + ChkBoxIsBlocked.Checked + ", Roles:" + roleslist;

                if (Request.ServerVariables["LOGON_USER"] != null)
                {
                    audit.WindowsUser = Request.ServerVariables["LOGON_USER"];
                }
                audit.IPAddress = Request.UserHostAddress;
                db.Audits.InsertOnSubmit(audit);
                db.SubmitChanges();

                LoadData();
                MaintainScrollPositionOnPostBack = false;
            }


            catch (Exception objException)
            {
                if (objException.Message.Trim() == "The E-mail supplied is invalid.")
                {
                    string script = "alertify.alert('" + ltrEmailExists.Text + "');";
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", script, true);
                }
                db = new CSLOrderingARCBAL.LinqToSqlDataContext();
                db.USP_SaveErrorDetails(Request.Url.ToString(), "btnSave_Click", Convert.ToString(objException.Message), Convert.ToString(objException.InnerException), Convert.ToString(objException.StackTrace), "", HttpContext.Current.Request.UserHostAddress, false, Convert.ToString(HttpContext.Current.Session[enumSessions.User_Id.ToString()]));
            }
        }
        else
        {
            string script = "alertify.alert('" + ltrFill.Text + "');";
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "alert", script, true);
            MaintainScrollPositionOnPostBack = false;
        }
    }