public ActionResult ChangePass(int id, string user, string passOld, string passNew) { try { crm_Users crm_Users = _userService.Select().Where(x => x.ID.Equals(id)).FirstOrDefault(); bool login = EncryptProvider.EncryptPassword(passOld, crm_Users.PasswordSalt).Equals(crm_Users.Password); if (login && crm_Users.Username.Equals(user)) { string encryptPassword = ""; string passwordSalt = ""; passwordSalt = EncryptProvider.GenerateSalt(); encryptPassword = EncryptProvider.EncryptPassword(passNew, passwordSalt); crm_Users.PasswordSalt = passwordSalt; crm_Users.Password = encryptPassword; crm_Users.ObjectState = ObjectState.Modified; _userService.Update(crm_Users); _unitOfWork.SaveChanges(); } else { return(Json(new { Status = -1, Message = "Password old not correct!" })); } _helper.InsertLogActive(_logService, _unitOfWork, "User", "change password succes.", 2, true); return(Json(new { Status = 0 })); } catch (Exception ex) { _helper.InsertLogActive(_logService, _unitOfWork, "User", "change password :" + ex.ToString(), 2, false); return(null); } }
public ActionResult DeleteConfirmed(int id) { try { //check role follow view int maxLogin = userInfo.BitMask.Max(i => i); int maxUser = GetbitMaskUser(id, maxLogin); if (maxLogin <= maxUser) { return(Json(new { Status = -1, Message = "You do not have permission!" })); } crm_Users crm_Users = _userService.Select().Where(x => x.ID.Equals(id)).FirstOrDefault(); List <crm_UserRoles> crm_UserRoles = _userRoleService.Select().Where(x => x.UserID.Value.Equals(id)).ToList(); foreach (var item in crm_UserRoles) { _userRoleService.Delete(item); } _userService.Delete(crm_Users); _unitOfWork.SaveChanges(); _helper.InsertLogActive(_logService, _unitOfWork, "User", "Delete user success.", 3, true); return(Json(new { Status = 0 })); } catch (Exception ex) { _helper.InsertLogActive(_logService, _unitOfWork, "User", "Delete user :"******"delete fail." })); } }
public ActionResult EditLogo(string userId, string linked, string twitter, string facebook, string google) { try { int id = Convert.ToInt32(userId); //var crm_users = _userService.Find(id); crm_Users crm_Users = _userService.ODataQueryable().Where(x => x.ID.Equals(id)).FirstOrDefault(); if (!string.IsNullOrEmpty(_logoModel.FileName)) { crm_Users.Image = _logoModel.FileName; _logoModel = null; } crm_Users.LinkedURL = linked; crm_Users.FacebookURL = facebook; crm_Users.TwitterURL = twitter; crm_Users.GoogleplusURL = google; crm_Users.UpdatedDate = DateTime.Now; crm_Users.ObjectState = ObjectState.Modified; _userService.Update(crm_Users); _unitOfWork.SaveChanges(); _helper.InsertLogActive(_logService, _unitOfWork, "User", "update avatar of user success", 2, true); return(Content("Update success!")); } catch (Exception ex) { _helper.InsertLogActive(_logService, _unitOfWork, "User", "update avatar of user :"******"Update fail!")); } }
public static crm_Users ToEntity(this UsersModel model) { var _userEntity = new crm_Users(); AutoMapper.Mapper.CreateMap <UsersModel, crm_Users>(); AutoMapper.Mapper.Map(model, _userEntity); return(_userEntity); }
/// <summary> /// /////////////////// /// </summary> /// <param name="entity"></param> /// <returns></returns> public static UsersModel ToModel(this crm_Users entity) { var _usertModel = new UsersModel(); AutoMapper.Mapper.CreateMap <crm_Users, UsersModel>(); AutoMapper.Mapper.Map(entity, _usertModel); return(_usertModel); }
public ActionResult DeleteRecords(List <int> ids) { //optimize later foreach (var id in ids) { crm_Users crm_Users = _userService.Find(id); _userService.Delete(crm_Users); _unitOfWork.SaveChanges(); } // return 0 /1 return(Json(new { Status = 0 })); }
// GET: /Tenant/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } //crm_Users crm_Users = _userService.Find(id); //check role follow edit int maxLogin = userInfo.BitMask.Max(i => i); int maxUser = GetbitMaskUser(id.Value, maxLogin); if (maxUser == -1) { return(RedirectToAction("AccessDeny", "Roles")); } if (maxLogin == maxUser) { if (!userInfo.ID.Equals(id)) { return(RedirectToAction("AccessDeny", "Roles")); } } else if (maxLogin < maxUser) { return(RedirectToAction("AccessDeny", "Roles")); } //end crm_Users crm_Users = _userService.ODataQueryable().Where(x => x.ID.Equals(id.Value)).ToList().FirstOrDefault(); UsersModel usersModel = crm_Users.ToModel(); var temp = _roleService.ODataQueryable().Where(x => x.TenantId.Equals(usersModel.TenantId) && x.PermissionType <= maxLogin); if (usersModel.TenantId.Equals(0)) { usersModel.crm_Roles = temp.Where(x => x.PermissionType >= (int)UserGroupEnum.Operator).ToList(); } else { usersModel.crm_Roles = temp.Where(x => x.PermissionType < (int)UserGroupEnum.Operator).ToList(); } usersModel.crm_UserRoles = _userRoleService.ODataQueryable().Where(x => x.UserID.Value.Equals(id.Value)); usersModel.SelectedRoleIds = usersModel.crm_UserRoles.FirstOrDefault().RoleID.Value; if (usersModel == null) { return(HttpNotFound()); } return(View(usersModel)); }
// GET: /Tenant/Delete/5 public ActionResult Delete(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } crm_Users crm_Users = _userService.Find(id); if (crm_Users == null) { return(HttpNotFound()); } return(View(crm_Users)); }
public ActionResult List([DataSourceRequest] DataSourceRequest request) { int total = 0; SortDescriptor sortDescriptor = (request.Sorts != null && request.Sorts.Count > 0) ? request.Sorts.FirstOrDefault() : new SortDescriptor("CreatedDate", ListSortDirection.Descending); sortDescriptor.Member = sortDescriptor.Member ?? "TenantName"; Func <IQueryable <crm_Tenants>, IOrderedQueryable <crm_Tenants> > order; var data = new List <crm_Tenants>(); switch (sortDescriptor.Member) { case "TenantName": if (sortDescriptor.SortDirection == ListSortDirection.Ascending) { order = x => x.OrderBy(y => y.TenantName); } else { order = x => x.OrderByDescending(y => y.TenantName); } break; case "TenantNameAlias": if (sortDescriptor.SortDirection == ListSortDirection.Ascending) { order = x => x.OrderBy(y => y.TenantNameAlias); } else { order = x => x.OrderByDescending(y => y.TenantNameAlias); } break; default: if (sortDescriptor.SortDirection == ListSortDirection.Ascending) { order = x => x.OrderBy(y => y.CreatedDate); } else { order = x => x.OrderByDescending(y => y.CreatedDate); } break; } data = _tenantService.Select(null, order, null, request.Page, request.PageSize).ToList(); var _lstTenantModel = new List <TenantModel>(); var _userEntity = new crm_Users(); var _tenantModel = new TenantModel(); foreach (var item in data) { _tenantModel = item.ToModel(); _userEntity = _userService.GetUserById(item.AssignedUserId); if (_userEntity != null) { _tenantModel.ContactName = _userEntity.FullName; _tenantModel.ContactPhone = _userEntity.Phone; _tenantModel.ContactEmail = _userEntity.Email; } _lstTenantModel.Add(_tenantModel); } total = _tenantService.Select(null, order, null, null, null).Count(); ViewBag.total = total; var result = new DataSourceResult() { Data = _lstTenantModel, Total = total }; return(Json(result)); }
public ActionResult Create(TenantModel tenantModel) { if (ModelState.IsValid) { var _tenantEntity = tenantModel.ToEntity(); _tenantEntity.Active = true; _tenantEntity.CreatedDate = DateTime.Now; _tenantEntity.CreatedBy = _userInfo.ID; _tenantEntity.ModifiedBy = _userInfo.ID; _tenantEntity.ModifiedDate = DateTime.Now; _tenantEntity.DbName = "MultiOrg_" + _tenantEntity.TenantNameAlias; try { if (_tenantService.CheckAlias(_tenantEntity.TenantNameAlias)) { // create directory folder tenant _pathFiles = "/tenants/" + tenantModel.TenantNameAlias; var _newPath = Server.MapPath(_pathFiles); if (!Directory.Exists(_newPath)) { Directory.CreateDirectory(_newPath); } // create file default var _fileDefault = "default.png"; var _sourceFile = Path.Combine(Server.MapPath(_tempFiles), _fileDefault); var _destinationFile = Path.Combine(Server.MapPath(_pathFiles), _fileDefault); System.IO.File.Copy(_sourceFile, _destinationFile); if (!string.IsNullOrEmpty(_logoModel.FileName)) { _tenantEntity.CompanyLogo = _pathFiles + "/" + _logoModel.FileName; //move a file from temps file to tenant folder _sourceFile = Path.Combine(Server.MapPath(_tempFiles), _logoModel.FileName); _destinationFile = Path.Combine(Server.MapPath(_pathFiles), _logoModel.FileName); if (System.IO.File.Exists(_destinationFile)) { System.IO.File.Delete(_destinationFile); } System.IO.File.Move(_sourceFile, _destinationFile); _logoModel = null; } else { _tenantEntity.CompanyLogo = _pathFiles + "/" + _fileDefault; } // insert new tenant _tenantService.Insert(_tenantEntity); _unitOfWork.SaveChanges(); // write log _helper.InsertLogActive(_logService, _unitOfWork, "Tenants", "Insert new tenant", 1, true); var result = new SqlQueryExcute().CreateDBByTenant(_tenantEntity.DbName, _tenantEntity.DbUsername, _tenantEntity.DbPassword); // add new user var _tenantId = _tenantService.GetTanentByAlias(_tenantEntity.TenantNameAlias).TenantId; if (_tenantId > 0) { try { var _newUser = new crm_Users(); _newUser.Username = tenantModel.Username ?? _tenantEntity.DbUsername; string encryptPassword = ""; string passwordSalt = ""; passwordSalt = EncryptProvider.GenerateSalt(); encryptPassword = EncryptProvider.EncryptPassword(tenantModel.Password, passwordSalt); _newUser.PasswordSalt = passwordSalt; _newUser.Password = encryptPassword; _newUser.TenantId = _tenantId; _newUser.CreatedDate = DateTime.Now; _newUser.Active = true; _newUser.Email = tenantModel.ContactEmail ?? _tenantEntity.Email; _newUser.DisplayName = tenantModel.ContactName ?? _tenantEntity.TenantName; _newUser.FullName = tenantModel.ContactName ?? _tenantEntity.TenantName; _newUser.FacebookURL = _tenantEntity.FacebookURL; _newUser.TwitterURL = _tenantEntity.TwitterURL; _newUser.GoogleplusURL = _tenantEntity.GoogleplusURL; _newUser.LinkedURL = _tenantEntity.LinkedURL; _newUser.Image = _tenantEntity.CompanyLogo; _newUser.Phone = tenantModel.ContactPhone; _newUser.Mobile = tenantModel.MobilePhone; _userService.Insert(_newUser); _unitOfWork.SaveChanges(); _helper.InsertLogActive(_logService, _unitOfWork, "Users", "Insert user tenant admin", 1, true); // add tenant admin role var _userId = _userService.GetUserByUsername(_newUser.Username).ID; if (_userId > 0) { try { // AssignedUserId var _ownTenantEntity = _tenantService.Find(_tenantId); _ownTenantEntity.AssignedUserId = _userId; _ownTenantEntity.ObjectState = ObjectState.Modified; _tenantService.Update(_ownTenantEntity); _unitOfWork.SaveChanges(); _helper.InsertLogActive(_logService, _unitOfWork, "Tenant", "Assigned user for tenant", 2, true); // create group role var _roleEntity = new crm_Roles(); //Console.WriteLine(((WeekDays)1).ToString()); // TenantAdmin = 512 _roleEntity.RoleName = UserGroupEnum.TenantAdmin.ToString(); _roleEntity.Active = true; _roleEntity.MaskPermission = (int)UserGroupEnum.TenantAdmin + 15; _roleEntity.TenantId = _tenantId; _roleEntity.PermissionType = (int)UserGroupEnum.TenantAdmin; _roleService.Insert(_roleEntity); _unitOfWork.SaveChanges(); // add Manager = 256 _roleEntity = new crm_Roles(); _roleEntity.RoleName = UserGroupEnum.Manager.ToString(); _roleEntity.Active = true; _roleEntity.MaskPermission = (int)UserGroupEnum.Manager + 15; _roleEntity.TenantId = _tenantId; _roleEntity.PermissionType = (int)UserGroupEnum.Manager; _roleService.Insert(_roleEntity); _unitOfWork.SaveChanges(); // add Support = 128 _roleEntity = new crm_Roles(); _roleEntity.RoleName = UserGroupEnum.Support.ToString(); _roleEntity.Active = true; _roleEntity.MaskPermission = (int)UserGroupEnum.Support + 15; _roleEntity.TenantId = _tenantId; _roleEntity.PermissionType = (int)UserGroupEnum.Support; _roleService.Insert(_roleEntity); _unitOfWork.SaveChanges(); // Marketing=64 _roleEntity = new crm_Roles(); _roleEntity.RoleName = UserGroupEnum.Marketing.ToString(); _roleEntity.Active = true; _roleEntity.MaskPermission = (int)UserGroupEnum.Marketing + 15; _roleEntity.TenantId = _tenantId; _roleEntity.PermissionType = (int)UserGroupEnum.Marketing; _roleService.Insert(_roleEntity); _unitOfWork.SaveChanges(); // Sales = 32 _roleEntity = new crm_Roles(); _roleEntity.RoleName = UserGroupEnum.Sales.ToString(); _roleEntity.Active = true; _roleEntity.MaskPermission = (int)UserGroupEnum.Sales + 15; _roleEntity.TenantId = _tenantId; _roleEntity.PermissionType = (int)UserGroupEnum.Sales; _roleService.Insert(_roleEntity); _unitOfWork.SaveChanges(); // NormalUser = 16 _roleEntity = new crm_Roles(); _roleEntity.RoleName = UserGroupEnum.NormalUser.ToString(); _roleEntity.Active = true; _roleEntity.MaskPermission = (int)UserGroupEnum.NormalUser + 15; _roleEntity.TenantId = _tenantId; _roleEntity.PermissionType = (int)UserGroupEnum.NormalUser; _roleService.Insert(_roleEntity); _unitOfWork.SaveChanges(); // map role tenant admin var _newRole = new crm_UserRoles(); _newRole.RoleID = _roleService.GetRoleIdByPermisstionType(_tenantId, (int)UserGroupEnum.TenantAdmin); _newRole.UserID = _userId; _userRoleService.Insert(_newRole); _unitOfWork.SaveChanges(); _helper.InsertLogActive(_logService, _unitOfWork, "User Role", "Insert role tenant admin for user", 1, true); } catch { _helper.InsertLogActive(_logService, _unitOfWork, "User Role", "Insert role tenant admin for user", 1, false); } } } catch { _helper.InsertLogActive(_logService, _unitOfWork, "Users", "Insert user tenant admin", 1, false); } } } MessageBoxModel.ShowMessage = "Add tenant " + _tenantEntity.TenantName + " success!"; return(RedirectToAction("Index")); } catch { _helper.InsertLogActive(_logService, _unitOfWork, "Tenants", "Insert new tenant", 1, false); return(View(tenantModel)); } } return(View(tenantModel)); }
public async Task <ActionResult> LogOn(LoginModels model, string returnUrl) { //variables crm_Users userLogin = null; crm_Tenants tenant = null; try { TempData["ShowCaptcha"] = ShowCaptcha; if (ModelState.IsValid) { if (ShowCaptcha.Trim().ToLower() == "false") { model.Captcha = Session["Captcha"].ToString(); } if (model.Captcha == Session["Captcha"].ToString()) { userLogin = _userService.GetUserByUsername(model.UserName); if (userLogin == null) { ModelState.AddModelError("", Caching.GetValue("Global.Login.lblUserNameOrPasswordError")); return(View("Index", model)); } //Encrypt password string passwordEncrypt = EncryptProvider.EncryptPassword(model.Password, userLogin.PasswordSalt); List <crm_Users> user = new List <crm_Users>(); user = _userService.CheckUserLogin(model.UserName, passwordEncrypt); if (user.Count > 0) { FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); userinfo.TenantAlias = _tenantService.GetTanentAliasByTenantId(user[0].TenantId); userinfo.ID = user[0].ID; userinfo.UserName = user[0].Username; userinfo.DisplayName = user[0].DisplayName; userinfo.Email = user[0].Email; userinfo.TenanID = user[0].TenantId; userinfo.FullName = user[0].FullName; userinfo.Image = user[0].Image; userinfo.Active = user[0].Active; var list = _roleService.GetUserGroupRoles(userinfo.ID).ToList(); userinfo.BitMask = new List <int>(); foreach (var item in list) { int tempBitMask = GlobalFunctions.GetBitMaskOfUser(item.MaskPermission.Value); userinfo.BitMask.Add(tempBitMask); CheckPermUser(tempBitMask, ref userinfo); } System.Web.HttpContext.Current.Session["UserInfo"] = userinfo; if (model.RememberMe) { HttpCookie faCookie = new HttpCookie("Remember") { Value = "1", Expires = DateTime.Now.AddDays(15) }; Response.Cookies.Add(faCookie); faCookie = new HttpCookie("Info") { Value = "{\"US\":\"" + model.UserName + "\", \"PW\":\"" + model.Password + "\"}", Expires = DateTime.Now.AddDays(15) }; Response.Cookies.Add(faCookie); } else { HttpCookie faCookie = new HttpCookie("Remember") { Value = "0", Expires = DateTime.Now.AddDays(15) }; Response.Cookies.Add(faCookie); } Session["LogOn"] = model.UserName; //check role bool isTenantAdmin = false; foreach (int bitMask in userinfo.BitMask) { if (bitMask == (int)UserGroupEnum.TenantAdmin) { isTenantAdmin = true; } } if (isTenantAdmin) { var _alias = _tenantService.GetTanentAliasByTenantId(userinfo.TenanID); var _tenantInfo = _tenantService.GetTanentByAlias(_alias); _entityHelper.SetConnectionString(ConfigurationManager.AppSettings["ServerName"].ToString(), _tenantInfo.DbName, _tenantInfo.DbUsername, _tenantInfo.DbPassword); } return(RedirectToRoute("Admin_Default", new { controller = "Dashboard", action = "Index" })); } else { ModelState.AddModelError("", Caching.GetValue("Global.Login.lblUserNameOrPasswordError")); } } else { ModelState.AddModelError("", Caching.GetValue("Global.Login.lblSecurityCodeError")); } } } catch (Exception e) { Console.Write(e.Message); } return(View("Index", model)); }
public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { return(RedirectToAction("Manage")); } if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await AuthenticationManager.GetExternalLoginInfoAsync(); if (info == null) { return(View("ExternalLoginFailure")); } crm_Users crm_user = new crm_Users(); crm_user.Username = model.UserName; crm_user.FullName = model.UserName; var provider = info.Login.LoginProvider; var url = info.Login.ProviderKey; switch (provider.ToLower()) { case "google": { crm_user.GoogleplusURL = url; break; } case "facebook": { crm_user.FacebookURL = url; break; } case "twitter": { crm_user.TwitterURL = url; break; } case "linkedin": { crm_user.LinkedURL = url; break; } } _userService.Insert(crm_user); int InResult = _unitOfWork.SaveChanges(); if (InResult == 1) { Session["LogOn"] = model.UserName; return(RedirectToRoute( "Admin_Default", new { controller = "Dashboard", action = "Index" })); } } ViewBag.ReturnUrl = returnUrl; return(RedirectToRoute( "Admin_Default", new { controller = "Dashboard", action = "Index" })); }
public async Task <ActionResult> ExternalLoginCallback(ExternalLoginConfirmationPasswordViewModel model, string returnUrl) { var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(); if (loginInfo == null) { return(RedirectToRoute( "Common_Default", new { controller = "Login", action = "Index" })); } //Update URL for user //Encrypt password crm_Users userLogin = new crm_Users(); userLogin = _userService.GetUserByUsername(model.UserName); if (userLogin == null) { TempData["Msg"] = "User not exists in the system"; return(RedirectToAction("Index", "Login")); } string passwordEncrypt = EncryptProvider.EncryptPassword(model.Password, userLogin.PasswordSalt); List <crm_Users> crm_user = new List <crm_Users>(); crm_user = _userService.CheckUserLogin(model.UserName, passwordEncrypt); if (crm_user.Count > 0) { var provider = loginInfo.Login.LoginProvider; var url = loginInfo.Login.ProviderKey; switch (provider.ToLower()) { case "google": { crm_user[0].GoogleplusURL = url; break; } case "facebook": { crm_user[0].FacebookURL = url; break; } case "twitter": { crm_user[0].TwitterURL = url; break; } case "linkedin": { crm_user[0].LinkedURL = url; break; } } //crm_user[0].ConfirmPassword = crm_user[0].Password; _userService.Update(crm_user[0]); int Result = _unitOfWork.SaveChanges(); if (Result > 0) { userinfo.ID = crm_user[0].ID; userinfo.UserName = crm_user[0].Username; userinfo.DisplayName = crm_user[0].DisplayName; userinfo.Email = crm_user[0].Email; userinfo.TenanID = crm_user[0].TenantId; userinfo.FullName = crm_user[0].FullName; userinfo.Image = crm_user[0].Image; userinfo.Active = crm_user[0].Active; var list = _roleService.GetUserGroupRoles(userinfo.ID).ToList(); userinfo.BitMask = new List <int>(); foreach (var item in list) { int tempBitMask = GlobalFunctions.GetBitMaskOfUser(item.MaskPermission.Value); userinfo.BitMask.Add(tempBitMask); CheckPermUser(tempBitMask, ref userinfo); } System.Web.HttpContext.Current.Session["UserInfo"] = userinfo; Session["LogOn"] = crm_user[0].Username; return(RedirectToRoute( "Admin_Default", new { controller = "Dashboard", action = "Index" })); } } TempData["Msg"] = "User not exists in the system"; return(RedirectToAction("Index", "Login")); }