public static void SaveSystemUser(SystemUser user)
 {
     using (var ctx = new BMIKidsEntities(ConnectionString))
     {
         try
         {
             ctx.SystemUsers.ApplyChanges(user);
             ctx.SaveChanges();
         }
         catch (Exception ex)
         {
             LogUtility.WriteEntryEventLog("SaveSystemUser_DataProvider_UpdateTransaction", ex, EventLogEntryType.Information);
             if (ex.InnerException != null)
                 throw ex.InnerException;
             throw;
         }
     }
 }
예제 #2
0
        protected void SaveBtn_Click(object sender, EventArgs e)
        {
            try
            {
                SystemUser user = null;
                if (UtilityMethod.GetRequestParameter("uid").IsInt32())
                {
                    int? userId = UtilityMethod.GetRequestParameter("uid").ToInt32();
                    user = SystemUser_DataProvider.GetSystemUser(UserId: userId).FirstOrDefault();
                }

                if (user != null)
                {
                    user.MarkAsModified();

                    user.SSOUserName = txtUserName.Text;
                    user.Name = txtFName.Text;
                    user.Family = txtLName.Text;
                    user.Active = ApprovedChkBox.Checked;

                    foreach (ListItem li in chkBoxObjectRoles.Items)
                    {
                        var RoleId = Convert.ToInt32(li.Value);
                        if (li.Selected)
                        {
                            SystemRole Role = SystemUser_DataProvider.GetRoles(RoleId).First();
                            if (user.SystemRoles.All(o => o.RoleId != Role.RoleId))
                                user.SystemRoles.Add(Role);
                        }
                        else
                        {
                            if (user.SystemRoles.Any(o => o.RoleId == RoleId))
                                user.SystemRoles.Remove(user.SystemRoles.First(o => o.RoleId == RoleId));
                        }
                    }
                    SystemUser_DataProvider.SaveSystemUser(user);
                }
                else
                {
                    user = new SystemUser
                                        {
                                            SSOUserName = txtUserName.Text.Trim(),
                                            Name = txtFName.Text,
                                            Family = txtLName.Text,
                                            Active = ApprovedChkBox.Checked,
                                        };
                    foreach (ListItem li in chkBoxObjectRoles.Items)
                    {
                        var RoleId = Convert.ToInt32(li.Value);
                        if (li.Selected)
                        {
                            SystemRole Role = SystemUser_DataProvider.GetRoles(RoleId).First();
                            user.SystemRoles.Add(Role);
                        }
                    }
                    SystemUser_DataProvider.SaveSystemUser(user);
                }

                Page.Response.Redirect("AdminUsers.aspx");
            }
            catch (ThreadAbortException) { }
            catch (Exception exp)
            {
                string msg;
                if (exp.InnerException != null && exp.InnerException.ToString().Contains("IX_SystemUser_UniqueUserName"))
                    msg = "این نام کاربری قبلا انتخاب شده است.";
                else
                    msg = exp.Message + " :::  " + exp.InnerException;
                lblError.Text = msg;
            }
        }