123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- using Blazored.LocalStorage;
- using Microsoft.AspNetCore.Components.Authorization;
- using System.Net.Http;
- using System.Net.Http.Json;
- using System.Threading.Tasks;
- using wispro.sp.web.Auth;
- using wispro.sp.web.Models;
- using wispro.sp.web.Utils;
- using System.Security.Claims;
- using System.Collections.Generic;
- using wispro.sp.entity;
- using System;
- using wispro.sp.share.webViewObject;
- using System.Text.Json;
- namespace wispro.sp.web.Services
- {
- public interface IUserService
- {
- //CurrentUser CurrentUser { get; set; }
- Task<List<Staff>> GetAll();
- Task<CurrentUser> GetUser();
- Task<List<Staff>> GetReviewers(int? itemId, int appealTypeId);
- Task<Staff> GetUser(int staffId);
- }
- public class UserService : IUserService
- {
- private readonly IHttpService _httpClient;
- private readonly JwtAuthenticationStateProvider _jwt;
- private readonly ILocalStorageService _localStorageService;
- //private CurrentUser _CurrentUser= new CurrentUser() { Name = "" };
- public UserService(IHttpService httpClient, ILocalStorageService localStorageService, AuthenticationStateProvider jwt)
- {
- _httpClient = httpClient;
- _localStorageService = localStorageService;
- _jwt = (JwtAuthenticationStateProvider)jwt;
- }
- public async Task<CurrentUser> GetUser()
- {
- CurrentUser _user = new CurrentUser();
- var tokenInLocalStorage = await _localStorageService.GetItemAsync<userToken>("authToken");
- if (tokenInLocalStorage == null)
- {
- _jwt.NotifyUserLogOut();
- return null;
- }
- var claims = JwtParser.ParseClaimsFromJwt(tokenInLocalStorage.Token);
- foreach (Claim claim in claims)
- {
- if (claim.Type == ClaimTypes.Name )
- {
- _user.Name = claim.Value;
- break;
- }
- if(claim.Type == ClaimTypes.Role)
- {
- _user.Roles.Add(claim.Value);
- }
- }
- _user.Userid = tokenInLocalStorage.UserId;
- //Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(_user));
- return _user;
- }
- public async Task<List<Staff>> GetAll()
- {
- try
- {
- var _StaffGrade = await _httpClient.Get<List<Staff>>($"Staff/GetAll");
- return _StaffGrade;
- }
- catch (Exception ex)
- {
- if (ex.Message.Contains("Unauthorized"))
- {
- _jwt.NotifyUserLogOut();
- }
- return null;
- }
- }
- public async Task<List<Staff>> GetReviewers(int? itemId, int appealTypeId)
- {
- try
- {
- var _StaffGrade = await _httpClient.Get<List<Staff>>($"Staff/GetReviewers?ItemId={itemId}&appealTypeId={appealTypeId}");
- return _StaffGrade;
- }
- catch (Exception ex)
- {
- if (ex.Message.Contains("Unauthorized"))
- {
- _jwt.NotifyUserLogOut();
- }
- return null;
- }
- }
- public async Task<Staff> GetUser(int staffId)
- {
- try
- {
- var staff = await _httpClient.Get<Staff>($"Staff/GetUser?Id={staffId}");
- return staff;
- }
- catch (Exception ex)
- {
- if (ex.Message.Contains("Unauthorized"))
- {
- _jwt.NotifyUserLogOut();
- }
- return null;
- }
- }
- }
- }
|