public ActionResult Edit(TenantModel model) { var tenant = _tenantRepository.GetById(model.Id); if (ModelState.IsValid) { tenant = model.ToEntity(tenant); //set id for Address if (tenant.AddressId > 0) { tenant.Address.Id = tenant.AddressId.Value; } //always set IsNew to false when saving tenant.IsNew = false; _tenantRepository.Update(tenant); //commit all changes this._dbContext.SaveChanges(); //notification SuccessNotification(_localizationService.GetResource("Record.Saved")); return(new NullJsonResult()); } else { return(Json(new { Errors = ModelState.SerializeErrors() })); } }
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)); }