public BlockDataUserCreateModel GetEntityById(int id) { BlockDataUserCreateModel output = new BlockDataUserCreateModel(); try { DBConnection.OpenDBConnect(); if (this.DBConnection.ERROR != null) { throw new Exception("Can't connect to db"); } //STEP1: ***************************************************************/ Dictionary <string, object> dicParas = new Dictionary <string, object>(); Dictionary <string, object> dicParaOuts = new Dictionary <string, object>(); dicParas.Add("USERID", id); DataSet dataSet = DBConnection.ExecSelectSP("SP_USER_GET_BY_ID", dicParas, ref dicParaOuts, true); //**********************TABLE: ROLE*************************************** DataTable table1 = dataSet.Tables[0]; foreach (DataRow row in table1.Rows) { output.Email = row["Email"].ToString(); output.Phone = row["Phone"].ToString(); output.IsAdmin = (bool)row["IsAdmin"]; output.UserName = (string)row["UserName"]; output.UserId = (int)row["UserId"]; output.DeptId = (int)row["deptID"]; } output.IsSuperAdmin = output.IsAdmin && (output.DeptId == 0); //**********************TABLE: ROLEMENU *********************************************** DataTable table2 = dataSet.Tables[1]; foreach (DataRow row in table2.Rows) { output.LstSelectedMenu.Add(Int32.Parse(row["menuid"].ToString())); } foreach (DataRow row in dataSet.Tables[2].Rows) { output.LstSelectedRole.Add(Int32.Parse(row["roleid"].ToString())); } //**********************TABLE: ROLE *********************************************** DataTable table3 = dataSet.Tables[2]; foreach (DataRow row in table3.Rows) { BI_Project.Models.EntityModels.EntityRoleModel entityRoleModel = new EntityRoleModel(); //entityRoleModel.Name = row["Name"].ToString(); entityRoleModel.RoleId = (int)row["RoleId"]; output.ListAllRoles.Add(entityRoleModel); } } catch (Exception ex) { this.ERROR = ex.ToString(); } finally { this.DBConnection.CloseDBConnect(); } return(output); }
public int Create(BlockDataUserCreateModel model, string passOld, string saltOld) { int output = 0; string _salt = ""; PasswordManager pwm = new PasswordManager(); try { Dictionary <string, object> dicParas = new Dictionary <string, object>(); Dictionary <string, object> dicParaOuts = new Dictionary <string, object>(); dicParaOuts.Add("result", ""); string LstOfMenuIds = model.StrAllowedMenus; string LstOfRoleIds = string.Join(",", model.LstSelectedRole); if (model.IsSuperAdmin) { model.IsAdmin = true; model.DeptId = 0; } if (model.Password != null) { //if(BlockDataUserCreateModel.IsValidString(model.Password) is false) //{ // throw new Exception("Mật khẩu phải chứa ít nhất 8 ký tự bao gồm ít nhất 1 kí tự là chữ viết thường, 1 kí tự là chữ viết hoa và 1 kí tự là số"); //} string passwordHashed = pwm.GetPasswordHashedAndGetSalt(model.Password, out _salt); model.Password = passwordHashed; model.Salt = _salt; } if (model.UserId > 0 && model.UserName != null && model.Email != null && model.Phone != null) { dicParas.Add("UserId", model.UserId); dicParas.Add("UserName", model.UserName); if (model.Password == null) { dicParas.Add("Password", passOld); dicParas.Add("Salt", saltOld); dicParas.Add("Email", model.Email); dicParas.Add("Phone", model.Phone); dicParas.Add("IsAdmin", model.IsAdmin); dicParas.Add("deptID", model.DeptId); dicParas.Add("LstOfMenuIds", LstOfMenuIds); dicParas.Add("LstOfRoleIds", LstOfRoleIds); dicParas.Add("FullName", model.FullName); } else { dicParas.Add("Password", model.Password); dicParas.Add("Salt", model.Salt); dicParas.Add("Email", model.Email); dicParas.Add("Phone", model.Phone); dicParas.Add("IsAdmin", model.IsAdmin); dicParas.Add("deptID", model.DeptId); dicParas.Add("LstOfMenuIds", LstOfMenuIds); dicParas.Add("LstOfRoleIds", LstOfRoleIds); dicParas.Add("FullName", model.FullName); } output = DBConnection.ExecSPNonQuery("SP_USER_INSERT_OR_UPDATE", dicParas, ref dicParaOuts, true); } else if (model.UserName != null && model.Password != null && model.Email != null && model.Phone != null) { dicParas.Add("UserId", model.UserId); dicParas.Add("UserName", model.UserName); dicParas.Add("Password", model.Password); dicParas.Add("Salt", model.Salt); dicParas.Add("Email", model.Email); dicParas.Add("Phone", model.Phone); dicParas.Add("IsAdmin", model.IsAdmin); dicParas.Add("deptID", model.DeptId); dicParas.Add("LstOfMenuIds", LstOfMenuIds); dicParas.Add("LstOfRoleIds", LstOfRoleIds); dicParas.Add("FullName", model.FullName); output = DBConnection.ExecSPNonQuery("SP_USER_INSERT_OR_UPDATE", dicParas, ref dicParaOuts, true); } else { output = -1; } if (DBConnection.ERROR != null) { throw new Exception(DBConnection.ERROR); } } catch (Exception ex) { this.ERROR = ex.ToString(); output = -1; } finally { if (this.IsCloseDBAfterExecute) { this.DBConnection.CloseDBConnect(); } } return(output); }