Beispiel #1
0
        public async Task <ActionResult> Create(CustomerUnitModel model)
        {
            if (ModelState.IsValid)
            {
                if (!IsUniqueName(model.Name))
                {
                    ModelState.AddModelError(nameof(model.Name), $"Namnet används redan för en annan enhet för denna myndighet");
                }
                else if (!_userService.IsUniqueEmail(model.Email))
                {
                    ModelState.AddModelError(nameof(model.Email), $"E-postadressen används redan i tjänsten");
                }
                else
                {
                    int?customerId = User.TryGetCustomerOrganisationId();
                    var unit       = new CustomerUnit();
                    unit.Create(_clock.SwedenNow, User.GetUserId(), User.TryGetImpersonatorId(), User.GetCustomerOrganisationId(), model.Name, model.Email, model.LocalAdministrator);
                    await _userService.LogCustomerUnitUserUpdateAsync(model.LocalAdministrator, User.GetUserId(), User.TryGetImpersonatorId());

                    await _dbContext.AddAsync(unit);

                    await _dbContext.SaveChangesAsync();

                    return(RedirectToAction(nameof(View), new { id = unit.CustomerUnitId }));
                }
            }
            return(View(model));
        }
Beispiel #2
0
 internal static CustomerUnitModel GetModelFromCustomerUnit(CustomerUnit unit, bool isCentralAdmin = false)
 {
     return(new CustomerUnitModel
     {
         CustomerUnitId = unit.CustomerUnitId,
         Name = unit.Name,
         Email = unit.Email,
         CreatedAt = unit.CreatedAt,
         CreatedBy = unit.CreatedByUser.FullName,
         IsActive = unit.IsActive,
         InactivatedAt = unit.InactivatedAt,
         InactivatedBy = unit.InactivatedByUser?.FullName ?? string.Empty,
         CustomerId = unit.CustomerOrganisationId,
         IsCentralAdministrator = isCentralAdmin
     });
 }
Beispiel #3
0
        /// <summary>
        /// Sumit Method to add Users when map the user to Tenant
        /// Adding the newly created User in some other Tenanats of the same Organization
        /// </summary>
        /// <param name="newTenantId"></param>
        /// <param name="sourceTenantId"></param>
        /// <param name="entityList"></param>
        /// <returns></returns>
        private async Task CloneTenantData(int newTenantId, int?sourceTenantId, List <string> entityList)
        {
            if (!ReferenceEquals(entityList, null))
            {
                bool        isRoleselected = false;
                List <Role> roles          = null;
                foreach (string entityName in entityList.OrderBy(p => p))
                {
                    switch (entityName)
                    {
                    case "Vendors":
                    {
                        List <VendorUnit> vendorList = null;
                        //Get vendor data from Tenanat of Source
                        using (_unitOfWorkManager.Current.SetTenantId(sourceTenantId))
                        {
                            if (await _vendorUnit.CountAsync(u => u.TenantId == newTenantId) == 0)
                            {
                                vendorList = await _vendorUnit.GetAll().Where(u => u.TenantId == sourceTenantId).ToListAsync();
                            }
                        }
                        //Inserting Vendor Data in DestinationTenant
                        using (_unitOfWorkManager.Current.SetTenantId(newTenantId))
                        {
                            if (!ReferenceEquals(entityList, null))
                            {
                                foreach (var vendor in vendorList)
                                {
                                    var vendorUnit = new VendorUnit();
                                    vendor.MapTo(vendorUnit);
                                    vendorUnit.TenantId           = newTenantId;
                                    vendorUnit.CreatorUserId      = null;
                                    vendorUnit.CreatorUserId      = null;
                                    vendorUnit.LastModifierUserId = null;
                                    await _vendorUnit.InsertAsync(vendorUnit);
                                }
                            }
                        }
                        break;
                    }

                    case "Users":
                    {
                        var userRoleList = new Dictionary <User, List <Role> >();
                        Dictionary <Role, IReadOnlyList <Permission> > rolePermissionList = new Dictionary <Role, IReadOnlyList <Permission> >();
                        using (_unitOfWorkManager.Current.SetTenantId(sourceTenantId))
                        {
                            if (await _userUnit.CountAsync(u => u.TenantId == newTenantId) == 0)
                            {
                                //getting the userd except default user(AppSupport)
                                var userList = await _userManager.Users.Include(p => p.Roles)
                                               .Where(u => u.TenantId == sourceTenantId && u.UserName != StaticUsers.UserName).ToListAsync();

                                //getting all Rles
                                var allRoleList = await _roleUnit.GetAll().ToListAsync();

                                foreach (var user in userList)
                                {
                                    var roleNumbers = string.Join(",", user.Roles.Select(p => p.RoleId).ToArray());
                                    var rolelist    = allRoleList
                                                      .Where(p => roleNumbers.Contains(p.Id.ToString())).ToList();
                                    userRoleList.Add(user, rolelist);
                                    var rolepermission = await GetRoleswithPermissions(rolelist);

                                    if (!rolePermissionList.ContainsKey(rolepermission.FirstOrDefault().Key))
                                    {
                                        rolePermissionList.Add(rolepermission.FirstOrDefault().Key, rolepermission.FirstOrDefault().Value);
                                    }
                                }
                            }
                        }
                        using (_unitOfWorkManager.Current.SetTenantId(newTenantId))
                        {
                            if (!ReferenceEquals(entityList, null))
                            {
                                List <Role> newRoles = null;
                                if (!isRoleselected)
                                {
                                    newRoles = await InsertRoles(rolePermissionList, newTenantId);
                                }
                                else
                                {
                                    newRoles = roles;
                                }

                                foreach (var user in userRoleList)
                                {
                                    var userUnit = new User();
                                    user.Key.MapTo(userUnit);
                                    userUnit.TenantId           = newTenantId;
                                    userUnit.CreatorUser        = null;
                                    userUnit.CreatorUserId      = null;
                                    userUnit.LastModifierUser   = null;
                                    userUnit.LastModifierUserId = null;
                                    var userroles = (from p in user.Value
                                                     join newrole in newRoles
                                                     on p.Name equals newrole.Name
                                                     select newrole).ToList();
                                    userUnit.Roles = null;
                                    long userId = await _userUnit.InsertAndGetIdAsync(userUnit);

                                    foreach (var userRole in userroles)
                                    {
                                        await _userRolRepository.InsertAsync(new UserRole
                                            {
                                                RoleId   = userRole.Id,
                                                UserId   = userId,
                                                TenantId = newTenantId
                                            });
                                    }
                                }
                            }
                        }
                        break;
                    }

                    case "Roles":
                    {
                        isRoleselected = true;
                        Dictionary <Role, IReadOnlyList <Permission> > rolePermissionList = new Dictionary <Role, IReadOnlyList <Permission> >();
                        using (_unitOfWorkManager.Current.SetTenantId(sourceTenantId))
                        {
                            if (await _roleUnit.CountAsync(u => u.TenantId == newTenantId) == 0)
                            {
                                //getting the roles except DefaultRoles(User,AppSupport)
                                var rollList = await _roleUnit.GetAll().Where(u => u.TenantId == sourceTenantId &&
                                                                              u.Name != StaticRoleNames.Tenants.Admin &&
                                                                              u.Name != StaticRoleNames.Tenants.User).ToListAsync();

                                rolePermissionList = await GetRoleswithPermissions(rollList);
                            }
                        }
                        using (_unitOfWorkManager.Current.SetTenantId(newTenantId))
                        {
                            if (!ReferenceEquals(entityList, null))
                            {
                                roles = await InsertRoles(rolePermissionList, newTenantId);
                            }
                        }
                        break;
                    }

                    case "ChartofAccounts":
                    {
                        List <CoaUnit> coaList = null;
                        using (_unitOfWorkManager.Current.SetTenantId(sourceTenantId))
                        {
                            if (await _coaUnit.CountAsync(u => u.TenantId == newTenantId) == 0)
                            {
                                //getting the coaList
                                coaList = await _coaUnit.GetAll().Where(u => u.TenantId == sourceTenantId && u.IsCorporate).ToListAsync();
                            }
                        }
                        using (_unitOfWorkManager.Current.SetTenantId(newTenantId))
                        {
                            if (!ReferenceEquals(entityList, null))
                            {
                                foreach (var coa in coaList)
                                {
                                    var coaUnit = new CoaUnit();
                                    coa.MapTo(coaUnit);
                                    coa.TenantId           = newTenantId;
                                    coa.CreatorUserId      = null;
                                    coa.LastModifierUserId = null;
                                    await _coaUnit.InsertAsync(coaUnit);
                                }
                            }
                        }
                        break;
                    }

                    case "ProjectChartofAccounts":
                    {
                        List <CoaUnit> coaList = null;
                        using (_unitOfWorkManager.Current.SetTenantId(sourceTenantId))
                        {
                            if (await _coaUnit.CountAsync(u => u.TenantId == newTenantId) == 0)
                            {
                                coaList = await _coaUnit.GetAll().Where(u => u.TenantId == sourceTenantId && !u.IsCorporate).ToListAsync();
                            }
                        }
                        using (_unitOfWorkManager.Current.SetTenantId(newTenantId))
                        {
                            if (!ReferenceEquals(entityList, null))
                            {
                                foreach (var coa in coaList)
                                {
                                    var coaUnit = new CoaUnit();
                                    coa.MapTo(coaUnit);
                                    coaUnit.TenantId      = newTenantId;
                                    coaUnit.CreatorUserId = null;
                                    await _coaUnit.InsertAsync(coaUnit);
                                }
                            }
                        }
                        break;
                    }

                    case "Employees":
                    {
                        List <EmployeeUnit> empList = null;
                        using (_unitOfWorkManager.Current.SetTenantId(sourceTenantId))
                        {
                            if (await _employeeUnit.CountAsync(u => u.TenantId == newTenantId) == 0)
                            {
                                empList = await _employeeUnit.GetAll().Where(u => u.TenantId == sourceTenantId).ToListAsync();
                            }
                        }
                        using (_unitOfWorkManager.Current.SetTenantId(newTenantId))
                        {
                            using (_unitOfWorkManager.Current.SetTenantId(newTenantId))
                            {
                                var employeeUnit = new EmployeeUnit();
                                if (!ReferenceEquals(entityList, null))
                                {
                                    foreach (var emp in empList)
                                    {
                                        emp.MapTo(employeeUnit);
                                        employeeUnit.TenantId           = newTenantId;
                                        employeeUnit.CreatorUserId      = null;
                                        employeeUnit.LastModifierUserId = null;
                                        await _employeeUnit.InsertAsync(employeeUnit);
                                    }
                                }
                            }
                        }
                        break;
                    }

                    case "Customers":
                    {
                        List <CustomerUnit> customerList = null;
                        using (_unitOfWorkManager.Current.SetTenantId(sourceTenantId))
                        {
                            if (await _customerUnit.CountAsync(u => u.TenantId == newTenantId) == 0)
                            {
                                customerList = await _customerUnit.GetAll().Where(u => u.TenantId == sourceTenantId).ToListAsync();
                            }
                        }
                        using (_unitOfWorkManager.Current.SetTenantId(newTenantId))
                        {
                            if (!ReferenceEquals(entityList, null))
                            {
                                var customerUnit = new CustomerUnit();
                                foreach (var customer in customerList)
                                {
                                    customer.MapTo(customerUnit);
                                    customerUnit.TenantId           = newTenantId;
                                    customerUnit.CreatorUserId      = null;
                                    customerUnit.LastModifierUserId = null;
                                    await _customerUnit.InsertAsync(customerUnit);
                                }
                            }
                        }
                        break;
                    }
                    }
                }
            }
        }