199 lines
10 KiB
C#
199 lines
10 KiB
C#
using System;
|
||
using System.Threading.Tasks;
|
||
using Sozsoft.Platform.Extensions;
|
||
using Microsoft.AspNetCore.Identity;
|
||
using Microsoft.Extensions.Options;
|
||
using Volo.Abp;
|
||
using Volo.Abp.DependencyInjection;
|
||
using Volo.Abp.Guids;
|
||
using Volo.Abp.Identity;
|
||
using Volo.Abp.MultiTenancy;
|
||
using Volo.Abp.PermissionManagement;
|
||
using IdentityRole = Volo.Abp.Identity.IdentityRole;
|
||
using IdentityUser = Volo.Abp.Identity.IdentityUser;
|
||
using Volo.Abp.Domain.Repositories;
|
||
using Sozsoft.Platform.Entities;
|
||
using Volo.Abp.TenantManagement;
|
||
|
||
namespace Sozsoft.Platform.Data.Seeds;
|
||
|
||
[Dependency(ReplaceServices = true)]
|
||
public class PlatformIdentityDataSeeder : IdentityDataSeeder
|
||
{
|
||
private readonly IPermissionGrantRepository _permissionGrantRepository;
|
||
private readonly ITenantRepository _tenantRepository;
|
||
private readonly IRepository<Branch, Guid> _branchRepository;
|
||
private readonly IRepository<BranchUsers, Guid> _branchUsersRepository;
|
||
|
||
public PlatformIdentityDataSeeder(
|
||
IGuidGenerator guidGenerator,
|
||
IIdentityRoleRepository roleRepository,
|
||
IIdentityUserRepository userRepository,
|
||
IRepository<Branch, Guid> branchRepository,
|
||
IRepository<BranchUsers, Guid> branchUsersRepository,
|
||
ILookupNormalizer lookupNormalizer,
|
||
IdentityUserManager userManager,
|
||
IdentityRoleManager roleManager,
|
||
IPermissionGrantRepository permissionGrantRepository,
|
||
ICurrentTenant currentTenant,
|
||
ITenantRepository tenantRepository,
|
||
IOptions<IdentityOptions> identityOptions
|
||
) : base(guidGenerator, roleRepository, userRepository, lookupNormalizer, userManager, roleManager, currentTenant, identityOptions)
|
||
{
|
||
this._permissionGrantRepository = permissionGrantRepository;
|
||
this._tenantRepository = tenantRepository;
|
||
this._branchRepository = branchRepository;
|
||
this._branchUsersRepository = branchUsersRepository;
|
||
}
|
||
|
||
public override async Task<IdentityDataSeedResult> SeedAsync(string adminEmail, string adminPassword, Guid? tenantId = null, string adminUserName = null)
|
||
{
|
||
Check.NotNullOrWhiteSpace(adminEmail, nameof(adminEmail));
|
||
Check.NotNullOrWhiteSpace(adminPassword, nameof(adminPassword));
|
||
|
||
using (CurrentTenant.Change(tenantId))
|
||
{
|
||
await IdentityOptions.SetAsync();
|
||
|
||
var result = new IdentityDataSeedResult();
|
||
|
||
var branchCode = "";
|
||
var branchName = "";
|
||
|
||
if (tenantId == null && !CurrentTenant.IsAvailable)
|
||
{
|
||
branchCode = PlatformConsts.Branches.BranchCode;
|
||
branchName = PlatformConsts.Branches.BranchName;
|
||
}
|
||
else
|
||
{
|
||
var tenant = await _tenantRepository.FindAsync(tenantId ?? CurrentTenant.Id.Value);
|
||
if (tenant != null)
|
||
{
|
||
branchCode = tenant.Name;
|
||
branchName = tenant.GetOrganizationName();
|
||
}
|
||
}
|
||
|
||
//Default Branch otomatik olarak oluşturuluyor.
|
||
var defaultBranch = await _branchRepository.FirstOrDefaultAsync(b => b.Code == branchCode);
|
||
if (defaultBranch == null)
|
||
{
|
||
var branchId = GuidGenerator.Create();
|
||
defaultBranch = await _branchRepository.InsertAsync(new Branch(branchId)
|
||
{
|
||
Code = branchCode,
|
||
Name = branchName,
|
||
VknTckn = PlatformConsts.Branches.BranchVknTckn,
|
||
TaxOffice = PlatformConsts.Branches.BranchTaxOffice,
|
||
MobileNumber = PlatformConsts.Branches.BranchMobileNumber,
|
||
Country = PlatformConsts.Branches.BranchCountry,
|
||
City = PlatformConsts.Branches.BranchCity,
|
||
District = PlatformConsts.Branches.BranchDistrict,
|
||
Township = PlatformConsts.Branches.BranchTownship,
|
||
Email = PlatformConsts.Branches.BranchEmail,
|
||
Website = PlatformConsts.Branches.BranchWebsite,
|
||
IsActive = PlatformConsts.Branches.BranchIsActive,
|
||
TenantId = tenantId,
|
||
}, autoSave: true);
|
||
}
|
||
|
||
var adminRole = await RoleRepository.FindByNormalizedNameAsync(LookupNormalizer.NormalizeName(PlatformConsts.AbpIdentity.User.AdminRoleName));
|
||
if (adminRole is null)
|
||
{
|
||
var roleId = GuidGenerator.Create();
|
||
adminRole = new IdentityRole(
|
||
roleId,
|
||
PlatformConsts.AbpIdentity.User.AdminRoleName,
|
||
tenantId
|
||
)
|
||
{
|
||
IsStatic = true,
|
||
IsPublic = true
|
||
};
|
||
|
||
(await RoleManager.CreateAsync(adminRole)).CheckErrors();
|
||
result.CreatedAdminRole = true;
|
||
}
|
||
|
||
adminUserName ??= PlatformConsts.AbpIdentity.User.AdminEmailDefaultValue;
|
||
var adminUser = await UserRepository.FindByNormalizedUserNameAsync(
|
||
LookupNormalizer.NormalizeName(adminUserName)
|
||
);
|
||
|
||
if (adminUser is null)
|
||
{
|
||
var userId = GuidGenerator.Create();
|
||
adminUser = new IdentityUser(
|
||
userId,
|
||
adminUserName,
|
||
adminEmail,
|
||
tenantId
|
||
)
|
||
{
|
||
Name = PlatformConsts.AbpIdentity.User.AdminNameDefaultValue,
|
||
Surname = PlatformConsts.AbpIdentity.User.AdminSurNameDefaultValue,
|
||
};
|
||
|
||
adminUser.SetIsVerified(true);
|
||
adminUser.SetEmailConfirmed(true);
|
||
adminUser.SetRocketUsername(PlatformConsts.AbpIdentity.User.AdminRocketUsernameDefaultValue);
|
||
adminUser.SetPhoneNumber(PlatformConsts.AbpIdentity.User.AdminPhoneNumberDefaultValue, adminUser.PhoneNumberConfirmed);
|
||
adminUser.SetWorkHour(PlatformConsts.AbpIdentity.User.AdminWorkHourDefaultValue);
|
||
adminUser.SetNationality(PlatformConsts.AbpIdentity.User.AdminNationalityDefaultValue);
|
||
adminUser.SetBloodType(PlatformConsts.AbpIdentity.User.AdminBloodTypeDefaultValue);
|
||
adminUser.SetEducationLevel(PlatformConsts.AbpIdentity.User.AdminEducationLevelDefaultValue);
|
||
adminUser.SetGraduationSchool(PlatformConsts.AbpIdentity.User.AdminGraduationSchoolDefaultValue);
|
||
adminUser.SetHomeAddress(PlatformConsts.AbpIdentity.User.AdminHomeAddressDefaultValue);
|
||
adminUser.SetBirthDate(PlatformConsts.AbpIdentity.User.AdminBirthDateDefaultValue);
|
||
adminUser.SetBirthPlace(PlatformConsts.AbpIdentity.User.AdminBirthPlaceDefaultValue);
|
||
adminUser.SetMaritalStatus(PlatformConsts.AbpIdentity.User.AdminMaritalStatusDefaultValue);
|
||
adminUser.SetDepartmentId(PlatformConsts.AbpIdentity.User.AdminDepartmentIdDefaultValue);
|
||
adminUser.SetJobPositionId(PlatformConsts.AbpIdentity.User.AdminJobPositionIdDefaultValue);
|
||
|
||
(await UserManager.CreateAsync(adminUser, adminPassword, validatePassword: false)).CheckErrors();
|
||
result.CreatedAdminUser = true;
|
||
|
||
(await UserManager.AddToRoleAsync(adminUser, PlatformConsts.AbpIdentity.User.AdminRoleName)).CheckErrors();
|
||
|
||
// BranchUsers tablosuna admin kullanicisi ekleniyor.
|
||
var defaultBranchUsers = await _branchUsersRepository.FirstOrDefaultAsync(bu => bu.UserId == adminUser.Id && bu.BranchId == defaultBranch.Id);
|
||
if (defaultBranchUsers is null)
|
||
{
|
||
await _branchUsersRepository.InsertAsync(new BranchUsers
|
||
{
|
||
BranchId = defaultBranch.Id,
|
||
UserId = adminUser.Id,
|
||
TenantId = tenantId
|
||
}, autoSave: true);
|
||
}
|
||
}
|
||
|
||
/*admin rolüne ait yetkiler otomatik atanıyor*/
|
||
var permissions = await _permissionGrantRepository.GetListAsync("R", PlatformConsts.AbpIdentity.User.AdminRoleName);
|
||
if (permissions.Count == 0)
|
||
{
|
||
await _permissionGrantRepository.InsertManyAsync(
|
||
[
|
||
new(Guid.NewGuid(), IdentityPermissions.Roles.Default, "R", PlatformConsts.AbpIdentity.User.AdminRoleName, tenantId),
|
||
new(Guid.NewGuid(), IdentityPermissions.Roles.Create, "R", PlatformConsts.AbpIdentity.User.AdminRoleName, tenantId),
|
||
new(Guid.NewGuid(), IdentityPermissions.Roles.Delete, "R", PlatformConsts.AbpIdentity.User.AdminRoleName, tenantId),
|
||
new(Guid.NewGuid(), IdentityPermissions.Roles.Update, "R", PlatformConsts.AbpIdentity.User.AdminRoleName, tenantId),
|
||
new(Guid.NewGuid(), IdentityPermissions.Roles.Default + ".Export", "R", PlatformConsts.AbpIdentity.User.AdminRoleName, tenantId),
|
||
new(Guid.NewGuid(), IdentityPermissions.Roles.ManagePermissions, "R", PlatformConsts.AbpIdentity.User.AdminRoleName, tenantId),
|
||
new(Guid.NewGuid(), IdentityPermissions.Users.Default, "R", PlatformConsts.AbpIdentity.User.AdminRoleName, tenantId),
|
||
new(Guid.NewGuid(), IdentityPermissions.Users.Create, "R", PlatformConsts.AbpIdentity.User.AdminRoleName, tenantId),
|
||
new(Guid.NewGuid(), IdentityPermissions.Users.Delete, "R", PlatformConsts.AbpIdentity.User.AdminRoleName, tenantId),
|
||
new(Guid.NewGuid(), IdentityPermissions.Users.Update, "R", PlatformConsts.AbpIdentity.User.AdminRoleName, tenantId),
|
||
new(Guid.NewGuid(), IdentityPermissions.Users.ManagePermissions, "R", PlatformConsts.AbpIdentity.User.AdminRoleName, tenantId),
|
||
new(Guid.NewGuid(), IdentityPermissions.Users.Default + ".Export", "R", PlatformConsts.AbpIdentity.User.AdminRoleName, tenantId),
|
||
new(Guid.NewGuid(), IdentityPermissions.Users.ManageRoles, "R", PlatformConsts.AbpIdentity.User.AdminRoleName, tenantId),
|
||
new(Guid.NewGuid(), IdentityPermissions.UserLookup.Default, "R", PlatformConsts.AbpIdentity.User.AdminRoleName, tenantId),
|
||
]);
|
||
}
|
||
|
||
return result;
|
||
}
|
||
}
|
||
}
|
||
|