2026-02-24 20:44:16 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Sozsoft.Platform.Entities;
|
|
|
|
|
|
using Sozsoft.Platform.Extensions;
|
|
|
|
|
|
using Sozsoft.Platform.Identity.Dto;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2026-04-28 12:43:19 +00:00
|
|
|
|
using OpenIddict.Abstractions;
|
2026-02-24 20:44:16 +00:00
|
|
|
|
using Volo.Abp.Application.Services;
|
|
|
|
|
|
using Volo.Abp.Domain.Repositories;
|
|
|
|
|
|
using Volo.Abp.Guids;
|
|
|
|
|
|
using Volo.Abp.Identity;
|
|
|
|
|
|
using Volo.Abp.PermissionManagement;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Sozsoft.Platform.Identity;
|
|
|
|
|
|
|
|
|
|
|
|
[Authorize]
|
|
|
|
|
|
public class PlatformIdentityAppService : ApplicationService
|
|
|
|
|
|
{
|
|
|
|
|
|
public IIdentityUserAppService IdentityUserAppService { get; }
|
|
|
|
|
|
private readonly IIdentityUserRepository identityUserRepository;
|
2026-04-28 12:43:19 +00:00
|
|
|
|
private readonly IIdentitySessionRepository identitySessionRepository;
|
|
|
|
|
|
private readonly IOpenIddictTokenManager openIddictTokenManager;
|
2026-02-24 20:44:16 +00:00
|
|
|
|
public IRepository<PermissionDefinitionRecord, Guid> permissionRepository { get; }
|
|
|
|
|
|
public IRepository<Branch, Guid> branchRepository { get; }
|
|
|
|
|
|
public IRepository<BranchUsers, Guid> branchUsersRepository { get; }
|
|
|
|
|
|
public IRepository<IdentityClaimType, Guid> claimTypesRepository { get; }
|
|
|
|
|
|
public IGuidGenerator guidGenerator { get; }
|
|
|
|
|
|
public IdentityUserManager UserManager { get; set; }
|
2026-04-26 19:05:19 +00:00
|
|
|
|
public IRepository<WorkHour, Guid> workHourRepository { get; }
|
2026-05-04 14:35:18 +00:00
|
|
|
|
public IRepository<Department, Guid> departmentRepository { get; }
|
|
|
|
|
|
public IRepository<JobPosition, Guid> jobPositionRepository { get; }
|
2026-02-24 20:44:16 +00:00
|
|
|
|
|
|
|
|
|
|
public PlatformIdentityAppService(
|
|
|
|
|
|
IIdentityUserAppService identityUserAppService,
|
|
|
|
|
|
IIdentityUserRepository identityUserRepository,
|
2026-04-28 12:43:19 +00:00
|
|
|
|
IIdentitySessionRepository identitySessionRepository,
|
|
|
|
|
|
IOpenIddictTokenManager openIddictTokenManager,
|
2026-02-24 20:44:16 +00:00
|
|
|
|
IRepository<PermissionDefinitionRecord, Guid> permissionRepository,
|
|
|
|
|
|
IRepository<Branch, Guid> branchRepository,
|
|
|
|
|
|
IRepository<BranchUsers, Guid> branchUsersRepository,
|
|
|
|
|
|
IRepository<IdentityClaimType, Guid> claimTypesRepository,
|
2026-04-26 19:05:19 +00:00
|
|
|
|
IRepository<WorkHour, Guid> workHourRepository,
|
2026-05-04 14:35:18 +00:00
|
|
|
|
IRepository<Department, Guid> departmentRepository,
|
|
|
|
|
|
IRepository<JobPosition, Guid> jobPositionRepository,
|
2026-02-24 20:44:16 +00:00
|
|
|
|
IGuidGenerator guidGenerator
|
|
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.IdentityUserAppService = identityUserAppService;
|
|
|
|
|
|
this.identityUserRepository = identityUserRepository;
|
2026-04-28 12:43:19 +00:00
|
|
|
|
this.identitySessionRepository = identitySessionRepository;
|
|
|
|
|
|
this.openIddictTokenManager = openIddictTokenManager;
|
2026-04-26 19:05:19 +00:00
|
|
|
|
this.workHourRepository = workHourRepository;
|
2026-05-04 14:35:18 +00:00
|
|
|
|
this.departmentRepository = departmentRepository;
|
|
|
|
|
|
this.jobPositionRepository = jobPositionRepository;
|
2026-02-24 20:44:16 +00:00
|
|
|
|
this.permissionRepository = permissionRepository;
|
|
|
|
|
|
this.branchRepository = branchRepository;
|
|
|
|
|
|
this.branchUsersRepository = branchUsersRepository;
|
|
|
|
|
|
this.claimTypesRepository = claimTypesRepository;
|
|
|
|
|
|
this.guidGenerator = guidGenerator;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<UserInfoViewModel> GetByIdAsync(Guid UserId)
|
|
|
|
|
|
{
|
|
|
|
|
|
var user = await identityUserRepository.GetAsync(UserId);
|
|
|
|
|
|
|
|
|
|
|
|
var userRoleNames = (await IdentityUserAppService.GetRolesAsync(user.Id)).Items.Select(r => r.Name).ToList();
|
|
|
|
|
|
var roles = ObjectMapper.Map<IReadOnlyList<IdentityRoleDto>, AssignedRoleViewModel[]>((await IdentityUserAppService.GetAssignableRolesAsync()).Items);
|
|
|
|
|
|
foreach (var role in roles)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (userRoleNames.Contains(role.Name))
|
|
|
|
|
|
{
|
|
|
|
|
|
role.IsAssigned = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var currentTenantId = CurrentTenant.Id.HasValue ? CurrentTenant.Id : null;
|
|
|
|
|
|
|
|
|
|
|
|
//Branch
|
|
|
|
|
|
var queryBranch = await branchUsersRepository.GetQueryableAsync();
|
|
|
|
|
|
var branchUsers = queryBranch.Where(a => a.UserId == UserId).Select(r => r.BranchId).ToList();
|
|
|
|
|
|
var branchList = await branchRepository.GetListAsync(a => a.TenantId == currentTenantId);
|
|
|
|
|
|
var branches = branchList.Select(branch => new AssignedBranchViewModel
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = branch.Id,
|
|
|
|
|
|
Name = branch.Name,
|
|
|
|
|
|
IsAssigned = branchUsers.Contains(branch.Id)
|
|
|
|
|
|
})
|
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
|
|
//Claim
|
|
|
|
|
|
var claimUsers = user.Claims;
|
|
|
|
|
|
var claimList = await claimTypesRepository.GetListAsync();
|
|
|
|
|
|
var claims = claimList.Select(claim =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var userClaim = claimUsers.FirstOrDefault(c => c.ClaimType == claim.Name);
|
|
|
|
|
|
return new AssignedClaimViewModel
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = userClaim?.Id ?? Guid.Empty,
|
|
|
|
|
|
ClaimType = claim.Name,
|
|
|
|
|
|
ClaimValue = userClaim?.ClaimValue,
|
|
|
|
|
|
IsAssigned = userClaim != null
|
|
|
|
|
|
};
|
|
|
|
|
|
}).ToArray();
|
|
|
|
|
|
|
2026-04-26 19:05:19 +00:00
|
|
|
|
var workHourList = await workHourRepository.GetListAsync();
|
|
|
|
|
|
var workHours = workHourList.Select(workHour => new AssignedWorkHourViewModel
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = workHour.Id,
|
|
|
|
|
|
Name = workHour.Name,
|
|
|
|
|
|
IsAssigned = workHourList.Contains(workHour)
|
|
|
|
|
|
}).ToArray();
|
2026-05-04 14:35:18 +00:00
|
|
|
|
|
|
|
|
|
|
var departmentList = await departmentRepository.GetListAsync();
|
|
|
|
|
|
var departments = (await departmentRepository.GetListAsync()).Select(department => new AssignedDepartmentViewModel
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = department.Id,
|
|
|
|
|
|
Name = department.Name,
|
|
|
|
|
|
IsAssigned = departmentList.Contains(department)
|
|
|
|
|
|
}).ToArray();
|
|
|
|
|
|
|
|
|
|
|
|
var jobPositionList = await jobPositionRepository.GetListAsync();
|
|
|
|
|
|
var jobPositions = (await jobPositionRepository.GetListAsync()).Select(jobPosition => new AssignedJobPoisitionViewModel
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = jobPosition.Id,
|
|
|
|
|
|
Name = jobPosition.Name,
|
|
|
|
|
|
DepartmentId = jobPosition.DepartmentId,
|
|
|
|
|
|
IsAssigned = jobPositionList.Contains(jobPosition)
|
|
|
|
|
|
}).ToArray();
|
|
|
|
|
|
|
2026-02-24 20:44:16 +00:00
|
|
|
|
return new UserInfoViewModel()
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = user.Id,
|
2026-05-27 22:30:13 +00:00
|
|
|
|
TenantId = user.TenantId,
|
2026-02-24 20:44:16 +00:00
|
|
|
|
UserName = user.UserName,
|
|
|
|
|
|
Name = user.Name,
|
|
|
|
|
|
Surname = user.Surname,
|
|
|
|
|
|
Roles = roles,
|
|
|
|
|
|
Branches = branches,
|
2026-05-04 14:35:18 +00:00
|
|
|
|
Departments = departments,
|
|
|
|
|
|
JobPositions = jobPositions,
|
2026-02-24 20:44:16 +00:00
|
|
|
|
Claims = claims,
|
2026-04-26 19:05:19 +00:00
|
|
|
|
WorkHours = workHours,
|
2026-02-24 20:44:16 +00:00
|
|
|
|
Email = user.Email,
|
|
|
|
|
|
PhoneNumber = user.PhoneNumber,
|
|
|
|
|
|
IsActive = user.IsActive,
|
|
|
|
|
|
TwoFactorEnabled = user.TwoFactorEnabled,
|
|
|
|
|
|
IsVerified = (bool)user.ExtraProperties["IsVerified"],
|
|
|
|
|
|
userRoleNames = userRoleNames,
|
|
|
|
|
|
LockoutEnabled = user.LockoutEnabled,
|
|
|
|
|
|
LockoutEnd = user.LockoutEnd,
|
2026-04-27 21:29:03 +00:00
|
|
|
|
LockUser = user.LockoutEnabled && user.LockoutEnd.HasValue && user.LockoutEnd.Value.DateTime > DateTime.Now,
|
2026-02-24 20:44:16 +00:00
|
|
|
|
LoginEndDate = user.GetLoginEndDate(),
|
|
|
|
|
|
ConcurrencyStamp = user.ConcurrencyStamp,
|
|
|
|
|
|
LastPasswordChangeTime = user.LastPasswordChangeTime,
|
|
|
|
|
|
|
|
|
|
|
|
EmailConfirmed = user.EmailConfirmed,
|
|
|
|
|
|
PhoneNumberConfirmed = user.PhoneNumberConfirmed,
|
|
|
|
|
|
AccessFailedCount = user.AccessFailedCount,
|
|
|
|
|
|
ShouldChangePasswordOnNextLogin = user.ShouldChangePasswordOnNextLogin,
|
|
|
|
|
|
CreationTime = user.CreationTime,
|
|
|
|
|
|
LastModificationTime = user.LastModificationTime,
|
2026-05-05 07:23:02 +00:00
|
|
|
|
|
2026-05-06 19:07:30 +00:00
|
|
|
|
}.MapIdentityExtraProperties(user);
|
2026-02-24 20:44:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-04 14:35:18 +00:00
|
|
|
|
public async Task UpdatePermissionAsync(UserInfoViewModel UserInfo)
|
|
|
|
|
|
{
|
|
|
|
|
|
var roles = UserInfo.Roles.Where(r => r.IsAssigned).Select(r => r.Name).ToArray();
|
|
|
|
|
|
|
|
|
|
|
|
var user = await UserManager.GetByIdAsync(UserInfo.Id);
|
|
|
|
|
|
await UserManager.SetRolesAsync(user, roles);
|
|
|
|
|
|
|
|
|
|
|
|
//Braches bu kısımda güncelleniyor.
|
|
|
|
|
|
var existingBranches = await branchUsersRepository.GetListAsync(x => x.UserId == user.Id);
|
|
|
|
|
|
foreach (var item in existingBranches)
|
|
|
|
|
|
{
|
|
|
|
|
|
await branchUsersRepository.DeleteAsync(item);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Yeni atamaları ekle
|
|
|
|
|
|
var assignedBranchIds = UserInfo.Branches
|
|
|
|
|
|
.Where(b => b.IsAssigned)
|
|
|
|
|
|
.Select(b => b.Id)
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var branchId in assignedBranchIds)
|
|
|
|
|
|
{
|
|
|
|
|
|
var branchUser = new BranchUsers
|
|
|
|
|
|
{
|
|
|
|
|
|
UserId = user.Id,
|
|
|
|
|
|
BranchId = branchId
|
|
|
|
|
|
};
|
|
|
|
|
|
await branchUsersRepository.InsertAsync(branchUser);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-24 20:44:16 +00:00
|
|
|
|
public async Task UpdateLockoutAsync(UserInfoViewModel UserInfo)
|
|
|
|
|
|
{
|
|
|
|
|
|
var user = await UserManager.GetByIdAsync(UserInfo.Id);
|
|
|
|
|
|
if (UserInfo.LockUser)
|
|
|
|
|
|
{
|
|
|
|
|
|
await UserManager.SetLockoutEnabledAsync(user, true);
|
2026-04-27 21:29:03 +00:00
|
|
|
|
await UserManager.SetLockoutEndDateAsync(user, DateTime.Now.AddYears(1000));
|
2026-02-24 20:44:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
await UserManager.SetLockoutEndDateAsync(user, null);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Admin Verification
|
|
|
|
|
|
user.SetIsVerified(UserInfo.IsVerified);
|
|
|
|
|
|
|
|
|
|
|
|
//Two Factor Enabled
|
|
|
|
|
|
await UserManager.SetTwoFactorEnabledAsync(user, UserInfo.TwoFactorEnabled);
|
|
|
|
|
|
|
|
|
|
|
|
//LoginEndDate
|
|
|
|
|
|
user.SetLoginEndDate(UserInfo.LoginEndDate);
|
2026-04-26 19:05:19 +00:00
|
|
|
|
user.SetWorkHour(UserInfo.WorkHour);
|
2026-02-24 20:44:16 +00:00
|
|
|
|
user.SetIsActive(UserInfo.IsActive);
|
2026-05-27 22:30:13 +00:00
|
|
|
|
user.SetLastPasswordChangeTime(UserInfo.LastPasswordChangeTime);
|
2026-02-24 20:44:16 +00:00
|
|
|
|
|
|
|
|
|
|
user.SetEmailConfirmed(UserInfo.EmailConfirmed);
|
|
|
|
|
|
user.SetPhoneNumberConfirmed(UserInfo.PhoneNumberConfirmed);
|
|
|
|
|
|
user.SetShouldChangePasswordOnNextLogin(UserInfo.ShouldChangePasswordOnNextLogin);
|
|
|
|
|
|
|
|
|
|
|
|
if (UserInfo.AccessFailedCount == 0 && user.AccessFailedCount > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
await UserManager.ResetAccessFailedCountAsync(user);
|
|
|
|
|
|
}
|
|
|
|
|
|
await UserManager.SetLockoutEnabledAsync(user, UserInfo.LockoutEnabled);
|
|
|
|
|
|
await UserManager.UpdateAsync(user);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task UpdateUserAsync(UserInfoViewModel UserInfo)
|
|
|
|
|
|
{
|
|
|
|
|
|
var user = await UserManager.GetByIdAsync(UserInfo.Id);
|
|
|
|
|
|
|
|
|
|
|
|
user.Name = UserInfo.Name;
|
|
|
|
|
|
user.Surname = UserInfo.Surname;
|
|
|
|
|
|
user.SetPhoneNumber(UserInfo.PhoneNumber, user.PhoneNumberConfirmed);
|
|
|
|
|
|
user.SetRocketUsername(UserInfo.RocketUsername);
|
2026-04-26 19:05:19 +00:00
|
|
|
|
user.SetWorkHour(UserInfo.WorkHour);
|
2026-05-04 12:14:16 +00:00
|
|
|
|
user.SetDepartmentId(UserInfo.DepartmentId);
|
|
|
|
|
|
user.SetJobPositionId(UserInfo.JobPositionId);
|
2026-05-05 07:23:02 +00:00
|
|
|
|
user.SetNationality(UserInfo.Nationality);
|
|
|
|
|
|
user.SetSskNo(UserInfo.SskNo);
|
|
|
|
|
|
user.SetHireDate(UserInfo.HireDate);
|
|
|
|
|
|
user.SetTerminationDate(UserInfo.TerminationDate);
|
|
|
|
|
|
user.SetIdentityNumber(UserInfo.IdentityNumber);
|
|
|
|
|
|
user.SetSerialNo(UserInfo.SerialNo);
|
|
|
|
|
|
user.SetProvince(UserInfo.Province);
|
|
|
|
|
|
user.SetDistrict(UserInfo.District);
|
|
|
|
|
|
user.SetVillage(UserInfo.Village);
|
|
|
|
|
|
user.SetVolumeNo(UserInfo.VolumeNo);
|
|
|
|
|
|
user.SetFamilySequenceNo(UserInfo.FamilySequenceNo);
|
|
|
|
|
|
user.SetSequenceNo(UserInfo.SequenceNo);
|
|
|
|
|
|
user.SetIssuedPlace(UserInfo.IssuedPlace);
|
|
|
|
|
|
user.SetIssuedDate(UserInfo.IssuedDate);
|
|
|
|
|
|
user.SetBirthPlace(UserInfo.BirthPlace);
|
|
|
|
|
|
user.SetBirthDate(UserInfo.BirthDate);
|
|
|
|
|
|
user.SetFatherName(UserInfo.FatherName);
|
|
|
|
|
|
user.SetMotherName(UserInfo.MotherName);
|
|
|
|
|
|
user.SetMaritalStatus(UserInfo.MaritalStatus);
|
|
|
|
|
|
user.SetMarriageDate(UserInfo.MarriageDate);
|
|
|
|
|
|
user.SetHomeAddress(UserInfo.HomeAddress);
|
|
|
|
|
|
user.SetEducationLevel(UserInfo.EducationLevel);
|
|
|
|
|
|
user.SetGraduationSchool(UserInfo.GraduationSchool);
|
|
|
|
|
|
user.SetBloodType(UserInfo.BloodType);
|
2026-02-24 20:44:16 +00:00
|
|
|
|
|
|
|
|
|
|
await UserManager.UpdateAsync(user);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<List<PermissionDefinitionRecord>> GetPermissionList()
|
|
|
|
|
|
{
|
|
|
|
|
|
var list = await permissionRepository.GetListAsync();
|
|
|
|
|
|
|
|
|
|
|
|
return [.. list.OrderBy(p => p.Name)];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task CreateClaimUserAsync(UserClaimModel input)
|
|
|
|
|
|
{
|
|
|
|
|
|
var user = await identityUserRepository.GetAsync(input.UserId);
|
|
|
|
|
|
|
|
|
|
|
|
user.AddClaim(guidGenerator, new Claim(input.ClaimType, input.ClaimValue));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task DeleteClaimUser(Guid id, Guid userId)
|
|
|
|
|
|
{
|
|
|
|
|
|
var user = await identityUserRepository.GetAsync(userId);
|
|
|
|
|
|
var claim = user.Claims.FirstOrDefault(a => a.Id == id);
|
|
|
|
|
|
|
|
|
|
|
|
user.Claims.Remove(claim);
|
|
|
|
|
|
}
|
2026-04-28 12:43:19 +00:00
|
|
|
|
|
|
|
|
|
|
public async Task KickUserAsync(Guid userId)
|
|
|
|
|
|
{
|
|
|
|
|
|
using (CurrentTenant.Change(CurrentTenant.Id))
|
|
|
|
|
|
{
|
|
|
|
|
|
// 1. AbpSessions temizle
|
|
|
|
|
|
var sessions = await identitySessionRepository.GetListAsync(userId: userId);
|
|
|
|
|
|
foreach (var session in sessions)
|
|
|
|
|
|
await identitySessionRepository.DeleteAsync(session);
|
|
|
|
|
|
|
|
|
|
|
|
// 2. OpenIddict tokenlarını revoke et
|
|
|
|
|
|
await foreach (var token in openIddictTokenManager.FindBySubjectAsync(userId.ToString()))
|
|
|
|
|
|
await openIddictTokenManager.TryRevokeAsync(token);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-02-24 20:44:16 +00:00
|
|
|
|
}
|