HRM-10/HRM.UI/ClientApp/src/app/_services/auth/auth.service.ts

288 lines
13 KiB
TypeScript
Raw Normal View History

2026-11-03 13:06:00 +06:00
import { Injectable } from '@angular/core';
import { User } from '../../_models/Authentication/user';
import { HttpClient } from '@angular/common/http';
import { JwtHelperService } from '@auth0/angular-jwt';
import { map } from 'rxjs/operators';
import { ApiService } from '../../app.api.service';
import { EnumConfigurationType, EnumRoleType, EnumSystemType, EnumUserType } from '../../_models/enums';
import { HRMNotificationService } from '../../app.notification.service';
import { Role, RolePermission } from '../../_models/Authentication/role';
import { SearchEmployee } from '../../_models/Employee/searchEmployee';
import { PayrollType } from '../../_models/Authentication/payrollType';
import { UserRole } from '../../_models/Authentication/UserRole';
import { UserAccessType } from '../../_models/Authentication/UserAccessType';
import { PasswordHistory } from '../../_models/Authentication/passwordHistory';
import { Router } from '@angular/router';
import { Bookmark } from '../../_models/Basic/bookmark';
import { Subject } from 'rxjs';
import { PendingJobDTO } from 'src/app/_models/Work-Flow/pendingJobDTO';
import { DataPermission } from 'src/app/_models/Basic/DataPermission';
import { BehaviorSubject } from 'rxjs';
2026-11-03 15:54:54 +06:00
import { IdleService } from 'src/app/_guards/idle.service';
2026-11-03 13:06:00 +06:00
@Injectable({ providedIn: 'root' })
export class AuthService {
baseUrl = ApiService.BASE_URL + 'Authentication/';
jwtHelper = new JwtHelperService();
decodedToken: any;
subject: Subject<Bookmark[]>;
subject2: Subject<PendingJobDTO[]>;
subject3: Subject<any[]> = new Subject<any[]>();
/** Emits once whenever the user logs out - shared services listen to reset per-user state. */
private logoutSource = new Subject<void>();
logout$ = this.logoutSource.asObservable();
private isFromSuperUserSubject = new BehaviorSubject<boolean>(false);
isFromSuperUser$ = this.isFromSuperUserSubject.asObservable();
setIsFromSuperUser(value: boolean) {
this.isFromSuperUserSubject.next(value);
}
getIsFromSuperUser(): boolean {
return this.isFromSuperUserSubject.value;
}
private isFromUserSubject = new BehaviorSubject<boolean>(false);
isFromUser$ = this.isFromUserSubject.asObservable();
setIsFromUser(value: boolean) {
this.isFromUserSubject.next(value);
}
// bookmarks: Bookmark[];
constructor(private http: HttpClient,
public apiService: ApiService ,
2026-11-03 15:54:54 +06:00
public notificationService: HRMNotificationService, private router: Router, private idleService: IdleService) {
2026-11-03 13:06:00 +06:00
this.subject = new Subject<Bookmark[]>();
this.subject2 = new Subject<PendingJobDTO[]>();
// this.loadBookmark();
// console.log(this.bookmarks);
}
login(model: any) {
return this.http.post(this.apiService.base_url + '/Authentication/login', model).pipe(
map((response: any) => {
const user = response;
if (user) {
2026-11-03 15:54:54 +06:00
if(user.mfaRequired){
ApiService.isMFA = true;
ApiService.AuthenticationMFASessionID = user. mfaSessionId;
}
else{
ApiService.isMFA = false;
ApiService.AuthenticationMFASessionID = '';
localStorage.setItem('token', user);
ApiService.AuthenticationToken = user;
//idle logout
this.idleService.stopTracking();
}
2026-11-03 13:06:00 +06:00
}
})
);
}
RemoveToBarPendingJob(tranid:number) {
var pendingJobDTOs: PendingJobDTO[] = JSON.parse(localStorage.getItem('pendingjobs'));
var index = pendingJobDTOs.findIndex(x => x.id == tranid);
pendingJobDTOs.splice(index, 1);
this.subject2.next(pendingJobDTOs);
localStorage.removeItem('pendingjobs');
localStorage.setItem('pendingjobs', JSON.stringify(pendingJobDTOs));
}
userPayrolltypeSwitch(param: any): any {
return this.apiService.httpPost<any>('/Authentication/userPayrolltypeSwitch', param);
}
SwtichUserMode(param: any): any {
return this.apiService.httpPost<any>('/Authentication/switchuser', param);
}
GetLogInUserType(): any {
return this.apiService.httpGet<EnumUserType>('/Authentication/GetLogInUserType');
}
GetCurrentUser(): any {
return this.apiService.httpGet<any>('/Authentication/GetCurrentUser');
}
getUserByEmail(email: string) {
return this.apiService.httpGet<User>('/Authentication/getUserByEmail/' + email);
}
GetUsers(usertype: EnumUserType, loginidid?: string, name?: string): any {
//const params = {
// LogInID: loginid == undefined ? '' : loginid,
// Name: name == undefined ? '' : name,
// userType: usertype
//};
const nname = this.apiService.getApiDefaultData(name);
const sloginid = this.apiService.getApiDefaultData(loginidid);
return this.apiService.httpGet<User[]>('/Authentication/GetUsers/' + usertype + '/' + loginidid + '/' + name);
}
GetAllEmpNotYetUser() {
return this.apiService.httpGet<User[]>('/Authentication/GetAllEmpNotYetUser');
}
logout() {
localStorage.removeItem('forgotpass');
localStorage.removeItem('bookmark');
localStorage.removeItem('token');
localStorage.removeItem('menuList');
localStorage.removeItem('pendingjobs');
localStorage.removeItem('notifications');
sessionStorage.removeItem('employeeCommonData');
this.logoutSource.next();
this.router.navigate(['/login']);
2026-11-03 17:03:07 +06:00
// Clear the HttpOnly JWToken cookie on the server; logout must never fail because of it.
this.apiService.httpPost<any>('/Authentication/logOut', {}).subscribe({ error: () => {} });
2026-11-03 13:06:00 +06:00
}
SaveUser(oUser: User): any {
return this.apiService.httpPost<User>('/Authentication/SaveUser', oUser);
}
ApproveUser(oUser: User): any {
return this.apiService.httpPost<User>('/Authentication/ApproveUser', oUser);
}
DoActiveAndIntacive(oUser: User): any {
return this.apiService.httpPost<User>('/Authentication/DoActiveAndIntacive', oUser);
}
GetAllUsers() {
return this.apiService.httpGet<User[]>('/Authentication/getAllUsers');
}
getBookmarks() {
return this.apiService.httpGet<Bookmark[]>('/Authentication/getBookmarks');
}
SaveBookmark(oBookmark: Bookmark): any {
return this.apiService.httpPost<Bookmark>('/Authentication/SaveBookmark', oBookmark);
}
deleteBookmark(param: any) {
return this.apiService.httpPost('/Authentication/deleteBookmark', param);
}
deleteBookmarkByMenuKey(param: any) {
return this.apiService.httpPost('/Authentication/deleteBookmarkByMenuKey', param);
}
getPendingJob(employeeId: number) {
return this.apiService.httpGet<PendingJobDTO[]>('/Workflow/getPendingJob' + '/' + employeeId);
}
markNotificationsAsRead(ids: number[]) {
return this.apiService.httpPost<any>('/Workflow/MarkNotificationsAsRead', ids);
}
loggedIn() {
const token = localStorage.getItem('token');
ApiService.AuthenticationToken = token;
return !this.jwtHelper.isTokenExpired(token);
}
register(user: User) {
return this.http.post(this.baseUrl + 'register', user);
}
loadMenu() {
return this.http.get(this.baseUrl + 'loadMenu');
}
getAllRoles() {
return this.apiService.httpGet<RolePermission[]>('/Authentication/getAllRoles');
}
getRole(roleID: number) {
return this.apiService.httpGet<RolePermission[]>('/Authentication/getRole/' + roleID);
}
getMenuesFromConfig() {
return this.apiService.httpGet<RolePermission[]>('/Authentication/getMenuesFromConfig');
}
saveRole(params: Role) {
return this.apiService.httpPost<number>('/Authentication/saveRole', params);
}
GetRolePermissionbyUserID() {
return this.apiService.httpGet<any>('/Authentication/GetRolePermissionbyUserID');
}
getUserRoles(roletype: EnumRoleType, userID?: number, roleid?: number) {
const nuserid = this.apiService.getApiDefaultIntData(userID);
const nroletype = this.apiService.getApiDefaultIntData(roleid);
return this.apiService.httpGet<UserRole[]>('/Authentication/getUserRoles/' + roletype + '/'
+ nuserid + '/' + nroletype);
}
GetEmpUserRole(searchemps: SearchEmployee[]) {
return this.apiService.httpPost<UserRole[]>('/Authentication/GetEmpUserRole', searchemps);
}
saveUserRole(params: any) {
return this.apiService.httpPost<number>('/Authentication/saveUserRole', params);
}
GetAllPayrollTypes() {
return this.apiService.httpGet<PayrollType[]>('/Authentication/GetAllPayrollTypes');
}
GetPayrollTypesByLoginID(loginID: string) {
return this.apiService.httpGet<PayrollType[]>('/Authentication/GetPayrollTypesByLoginID/' + loginID);
}
GetBenifitsProcessStatus() {
return this.apiService.httpGet<PayrollType[]>('/Authentication/GetBenifitsProcessStatus' );
}
getSwitchUserId(usertype: EnumUserType) {
return this.apiService.httpGet<any>('/Authentication/getSwitchUserId/'+ usertype);
}
GetPayrollTypeByLoginID() {
return this.apiService.httpGet<PayrollType>('/Authentication/GetPayrollTypeByLoginID');
}
getAdminPayrollTypes() {
return this.apiService.httpGet<PayrollType[]>('/Authentication/getAdminPayrollTypes');
}
GetUserAccessType(userid: number) {
return this.apiService.httpGet<UserAccessType[]>('/Authentication/GetUserAccessType/' + userid);
}
SaveUserAccessType(acesssTypes: UserAccessType[]) {
return this.apiService.httpPost<boolean>('/Authentication/SaveUserAccessType', acesssTypes);
}
changePassword(userData: any) {
return this.apiService.httpPost<any>('/Authentication/changePassword', userData);
}
changePasswordbyAdmin(userData: any) {
return this.apiService.httpPost<any>('/Authentication/changePasswordbyAdmin', userData);
}
forgotPassword(data: any) {
return this.apiService.httpPost<any>('/Authentication/forgotPassword', data);
}
getVersionNumber() {
return this.apiService.httpGet<string>('/Authentication/getVersionNumber');
}
GetUserByLoingID(loginid: string, systemType: EnumSystemType) {
return this.apiService.httpGet<User>('/Authentication/GetUserByLoingID/' + loginid + '/' + systemType);
}
GetUserByID(userid:number) {
return this.apiService.httpGet<User>('/Authentication/GetUserByID/' + userid);
}
//Below code is for Data
getUserByUserType(param: any): any {
return this.apiService.httpPost<any>('/Authentication/getUserByUserType', param);
}
addPermission(param: any) {
return this.apiService.httpPost<any>('/Authentication/addPermission', param);
}
loadDataPermission(userid: number, payrolltypeid:number) {
return this.apiService.httpGet<DataPermission[]>('/Authentication/loadDataPermission/' + userid + "/" + payrolltypeid);
}
DeletedataPermission(item:any) {
return this.apiService.httpPost<any> ('/Authentication/deletePermission' ,item);
}
getSSOStatus() {
return this.apiService.httpGet<any>('/Authentication/getSSOStatus');
}
GetOperationDate() {
return this.apiService.httpGet<Date>('/Authentication/getOperationDate');
}
SetExpectedSalaryandApprovalDate(expectedDate : Date, expectedApprovalDate : Date) {
const fmt = (d: Date) => d.getFullYear() + '-' + ('0' + (d.getMonth() + 1)).slice(-2) + '-' + ('0' + d.getDate()).slice(-2);
return this.apiService.httpGet<PayrollType[]>('/Authentication/SetExpectedSalaryandApprovalDate'+ '/' + fmt(expectedDate) + '/' + fmt(expectedApprovalDate));
}
2026-11-03 15:54:54 +06:00
VerifyMfa(model: any) {
return this.http.post(this.apiService.base_url + '/Authentication/verify-mfa', model).pipe(
map((response: any) => {
const user = response;
localStorage.setItem('token', user.userToken);
ApiService.AuthenticationToken = user.userToken;
})
);
}
ResendOTP(model: any): any {
return this.http.post(this.apiService.base_url + '/Authentication/resend-otp', model).pipe(
map((response: any) => {
const user = response;
if (user) {
if(user.mfaRequired){
ApiService.isMFA = true;
ApiService.AuthenticationMFASessionID = user. mfaSessionId;
}
}
})
);
}
2026-11-03 13:06:00 +06:00
}