Esempio n. 1
0
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int?depositId      = null;
        int?representantId = null;

        if (!String.IsNullOrEmpty(cboDeposit.SelectedValue))
        {
            depositId = Convert.ToInt32(cboDeposit.SelectedValue);
        }

        if (!String.IsNullOrEmpty(cboRepresentant.SelectedValue))
        {
            representantId = Convert.ToInt32(cboRepresentant.SelectedValue);
        }

        //
        //Update User
        //
        if (!String.IsNullOrEmpty(Request["UserId"]))
        {
            var originalUser = CompanyManager.GetUser(Company.CompanyId, Convert.ToInt32(Page.ViewState["UserId"]));

            if (originalUser.UserName != txtUserName.Text && CompanyManager.ExistsUserInCompany(Company.CompanyId, txtUserName.Text))
            {
                ShowError(Exception.ExistentMail);
                return;
            }

            CompanyManager.UpdateUser(Company.CompanyId, Convert.ToInt32(Page.ViewState["UserId"]), depositId, representantId, ucProfile.ProfileEntity, SaveUser());

            RefreshDeposit();
            RefreshCredentials();
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "parent.location='Users.aspx'", true);
            return;
        }

        //
        // Verifies if the User already exist in company
        //

        if (CompanyManager.ExistsUserInCompany(Company.CompanyId, txtUserName.Text))
        {
            ShowError(Exception.ExistentMail);
            return;
        }

        var oldUser = MembershipManager.GetUserByName(txtUserName.Text);

        if (oldUser != null)
        {
            CompanyManager.UpdateUser(Company.CompanyId, oldUser.UserId, depositId, representantId, ucProfile.ProfileEntity, SaveUser());

            RefreshCredentials();
            Response.Redirect("User.aspx?UserId=" + oldUser.UserId);
            return;
        }

        //
        // Insert user
        //

        var newUser = SaveUser();
        InsertCompanyStatus status = CompanyManager.InsertUser(Company.CompanyId, depositId, representantId,

                                                               newUser, ucProfile.ProfileEntity);

        switch (status)
        {
        case InsertCompanyStatus.InvalidPassword:
            ShowError(Exception.InvalidPassword);
            break;

        case InsertCompanyStatus.InvalidUser:
        case InsertCompanyStatus.DuplicateCNPJ:
        case InsertCompanyStatus.DuplicatedAdminEmail:
        case InsertCompanyStatus.DuplicatedUserName:
            ShowError(Exception.ExistentMail);
            break;

        case InsertCompanyStatus.Success:

            Response.Redirect("User.aspx?UserId=" + newUser.UserId);
            break;
        }
        RefreshCredentials();
    }