erp-platform/api/src/Kurs.Platform.DbMigrator/Seeds/PlatformIdentityDataSeeder.cs
2025-05-06 14:03:45 +03:00

122 lines
6.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Threading.Tasks;
using Kurs.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 static Kurs.Platform.Data.Seeds.SeedConsts;
using IdentityRole = Volo.Abp.Identity.IdentityRole;
using IdentityUser = Volo.Abp.Identity.IdentityUser;
namespace Kurs.Platform.Data.Seeds;
[Dependency(ReplaceServices = true)]
public class PlatformIdentityDataSeeder : IdentityDataSeeder
{
private readonly IPermissionGrantRepository permissionGrantRepository;
public PlatformIdentityDataSeeder(
IGuidGenerator guidGenerator,
IIdentityRoleRepository roleRepository,
IIdentityUserRepository userRepository,
ILookupNormalizer lookupNormalizer,
IdentityUserManager userManager,
IdentityRoleManager roleManager,
IPermissionGrantRepository permissionGrantRepository,
ICurrentTenant currentTenant,
IOptions<IdentityOptions> identityOptions
) : base(guidGenerator, roleRepository, userRepository, lookupNormalizer, userManager, roleManager, currentTenant, identityOptions)
{
this.permissionGrantRepository = permissionGrantRepository;
}
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 adminRole = await RoleRepository.FindByNormalizedNameAsync(LookupNormalizer.NormalizeName(PlatformConsts.AbpIdentity.User.AdminRoleName));
if (adminRole is null)
{
adminRole = new IdentityRole(
GuidGenerator.Create(),
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)
{
adminUser = new IdentityUser(
GuidGenerator.Create(),
adminUserName,
adminEmail,
tenantId
)
{
Name = PlatformConsts.AbpIdentity.User.AdminNameDefaultValue,
Surname = PlatformConsts.AbpIdentity.User.AdminSurNameDefaultValue,
};
adminUser.SetEmailConfirmed(true);
adminUser.SetIsVerified(true);
adminUser.SetRocketUsername(PlatformConsts.AbpIdentity.User.AdminEmailDefaultValue);
adminUser.SetPhoneNumber(PlatformConsts.AbpIdentity.User.AdminPhoneNumberDefaultValue, true);
(await UserManager.CreateAsync(adminUser, adminPassword, validatePassword: false)).CheckErrors();
result.CreatedAdminUser = true;
(await UserManager.AddToRoleAsync(adminUser, PlatformConsts.AbpIdentity.User.AdminRoleName)).CheckErrors();
}
/*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(), AbpIdentity.Permissions.Default, "R", PlatformConsts.AbpIdentity.User.AdminRoleName, tenantId),
new(Guid.NewGuid(), AbpIdentity.Permissions.Create, "R", PlatformConsts.AbpIdentity.User.AdminRoleName, tenantId),
new(Guid.NewGuid(), AbpIdentity.Permissions.Update, "R", PlatformConsts.AbpIdentity.User.AdminRoleName, tenantId),
new(Guid.NewGuid(), AbpIdentity.Permissions.Delete, "R", PlatformConsts.AbpIdentity.User.AdminRoleName, tenantId),
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.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.ManageRoles, "R", PlatformConsts.AbpIdentity.User.AdminRoleName, tenantId),
new(Guid.NewGuid(), IdentityPermissions.UserLookup.Default, "R", PlatformConsts.AbpIdentity.User.AdminRoleName, tenantId),
]);
}
return result;
}
}
}