using System; using System.Collections.Generic; using System.Linq; using System.Security.Claims; using _0_Framework.Infrastructure; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Http; using Newtonsoft.Json; namespace _0_Framework.Application; public class AuthHelper : IAuthHelper { private readonly IHttpContextAccessor _contextAccessor; public AuthHelper(IHttpContextAccessor contextAccessor) { _contextAccessor = contextAccessor; } public AuthViewModel CurrentAccountInfo() { var result = new AuthViewModel(); if (!IsAuthenticated()) return result; var claims = _contextAccessor.HttpContext.User.Claims.ToList(); result.Id = long.Parse(claims.FirstOrDefault(x => x.Type == "AccountId").Value); result.Username = claims.FirstOrDefault(x => x.Type == "Username")?.Value; result.ProfilePhoto = claims.FirstOrDefault(x => x.Type == "ProfilePhoto")?.Value; result.RoleId = long.Parse(claims.FirstOrDefault(x => x.Type == ClaimTypes.Role)?.Value); result.Fullname = claims.FirstOrDefault(x => x.Type == ClaimTypes.Name)?.Value; result.Role = claims.FirstOrDefault(x => x.Type == "RoleName")?.Value; result.ClientAriaPermission =claims.FirstOrDefault(x => x.Type == "ClientAriaPermission").Value; result.AdminAreaPermission = claims.FirstOrDefault(x => x.Type == "AdminAreaPermission").Value; result.PositionValue = !string.IsNullOrWhiteSpace(claims.FirstOrDefault(x => x.Type == "PositionValue")?.Value) ? int.Parse(claims.FirstOrDefault(x => x.Type == "PositionValue")?.Value) : 0; result.WorkshopList = Tools.DeserializeFromBsonList(claims.FirstOrDefault(x => x is { Type: "workshopList" })?.Value); result.WorkshopSlug = claims.FirstOrDefault(x => x is { Type: "WorkshopSlug" }).Value; result.Mobile = claims.FirstOrDefault(x => x is { Type: "Mobile" }).Value; result.SubAccountId = long.Parse(claims.FirstOrDefault(x => x.Type == "SubAccountId").Value); result.WorkshopName = claims.FirstOrDefault(x => x is { Type: "WorkshopName" })?.Value; return result; } public List GetPermissions() { if (!IsAuthenticated()) return new List(); var permissions = _contextAccessor.HttpContext.User.Claims.FirstOrDefault(x => x.Type == "permissions") ?.Value; return Tools.DeserializeFromBsonList(permissions); //Mahan } public long CurrentAccountId() { return IsAuthenticated() ? long.Parse(_contextAccessor.HttpContext.User.Claims.First(x => x.Type == "AccountId")?.Value) : 0; } public long CurrentSubAccountId() { return IsAuthenticated() ? long.Parse(_contextAccessor.HttpContext.User.Claims.First(x => x.Type == "SubAccountId")?.Value) : 0; } public string CurrentAccountMobile() { return IsAuthenticated() ? _contextAccessor.HttpContext.User.Claims.First(x => x.Type == "Mobile")?.Value : ""; } #region Vafa public void UpdateWorkshopSlugClaim(string newWorkshopSlug, string newWorkshopName) { var user = _contextAccessor.HttpContext.User; if (user.Identity.IsAuthenticated) { var claimsIdentity = (ClaimsIdentity)user.Identity; var existingClaimSlug = claimsIdentity.FindFirst("WorkshopSlug"); var existingClaimName = claimsIdentity.FindFirst("WorkshopName"); if (existingClaimSlug != null) { claimsIdentity.RemoveClaim(existingClaimSlug); } if (existingClaimName != null) { claimsIdentity.RemoveClaim(existingClaimName); } claimsIdentity.AddClaim(new Claim("WorkshopSlug", newWorkshopSlug)); claimsIdentity.AddClaim(new Claim("WorkshopName", newWorkshopName)); var authProperties = new AuthenticationProperties { ExpiresUtc = DateTimeOffset.UtcNow.AddDays(1) }; _contextAccessor.HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties); } } public string GetWorkshopSlug() { return CurrentAccountInfo().ClientAriaPermission == "true" ? _contextAccessor.HttpContext.User.Claims.First(x => x.Type == "WorkshopSlug")?.Value : ""; } public string GetWorkshopName() { var workshopName = _contextAccessor.HttpContext.User.Claims.FirstOrDefault(x => x.Type == "ClientAriaPermission")?.Value == "true"; if (workshopName) { return _contextAccessor.HttpContext.User.Claims.First(x => x.Type == "WorkshopName")?.Value; } return ""; } #endregion public string CurrentAccountRole() { if (IsAuthenticated()) return _contextAccessor.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Role)?.Value; return null; } public bool IsAuthenticated() { return _contextAccessor.HttpContext.User.Identity.IsAuthenticated; //var claims = _contextAccessor.HttpContext.User.Claims.ToList(); //if (claims.Count > 0) // return true; //return false; //return claims.Count > 0; } public void Signin(AuthViewModel account) { #region MahanChanges var permissions = account.Permissions is { Count: > 0 } ? Tools.SerializeToBson(account.Permissions) : ""; var workshopBson = account.WorkshopList is { Count: > 0 } ? Tools.SerializeToBson(account.WorkshopList) : ""; var slug = account.WorkshopSlug ?? ""; #endregion var claims = new List { new Claim("AccountId", account.Id.ToString()), new Claim(ClaimTypes.Name, account.Fullname), new Claim(ClaimTypes.Role, account.RoleId.ToString()), new Claim("Username", account.Username), // Or Use ClaimTypes.NameIdentifier new Claim("permissions", permissions), new Claim("Mobile", account.Mobile), new Claim("ProfilePhoto", account.ProfilePhoto ), new Claim("RoleName", account.RoleName), new Claim("SubAccountId", account.SubAccountId.ToString()), new Claim("AdminAreaPermission", account.AdminAreaPermission.ToString()), new Claim("ClientAriaPermission", account.ClientAriaPermission.ToString()), new Claim("IsCamera", "false"), new Claim("PositionValue",account.PositionValue.ToString()), //mahanChanges new("workshopList",workshopBson), new("WorkshopSlug",slug), new("WorkshopName",account.WorkshopName??"") }; var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); var authProperties = new AuthenticationProperties { ExpiresUtc = DateTimeOffset.UtcNow.AddDays(1) }; _contextAccessor.HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties); } #region Camera public void CameraSignIn(CameraAuthViewModel account) { var claims = new List { new Claim("AccountId", account.Id.ToString()), new Claim("Username", account.Username), // Or Use ClaimTypes.NameIdentifier new Claim("WorkshopId", account.WorkshopId.ToString()), new Claim("WorkshopName", account.WorkshopName), new Claim("Mobile", account.Mobile), new Claim("AccountId", account.AccountId.ToString()), new Claim("IsActiveString", account.IsActiveString), new Claim("IsCamera", "true"), }; var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); var authProperties = new AuthenticationProperties { //ExpiresUtc = DateTimeOffset.UtcNow.AddDays(30) ExpiresUtc = new DateTimeOffset(year: 2100, month: 1, day: 1, hour: 0, minute: 0, second: 0, offset: TimeSpan.Zero) }; _contextAccessor.HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties); } public CameraAuthViewModel CameraAccountInfo() { var result = new CameraAuthViewModel(); if (!IsAuthenticated()) return result; var claims = _contextAccessor.HttpContext.User.Claims.ToList(); result.Id = long.Parse(claims.FirstOrDefault(x => x.Type == "AccountId").Value); result.Username = claims.FirstOrDefault(x => x.Type == "Username")?.Value; result.WorkshopId = long.Parse(claims.FirstOrDefault(x => x.Type == "WorkshopId")?.Value); result.WorkshopName = claims.FirstOrDefault(x => x.Type == "WorkshopName").Value; result.Mobile = claims.FirstOrDefault(x => x.Type == "Mobile").Value; result.AccountId = long.Parse(claims.FirstOrDefault(x => x.Type == "AccountId")?.Value); result.IsActiveString = claims.FirstOrDefault(x => x.Type == "IsActiveString").Value; return result; } #endregion public void SignOut() { _contextAccessor.HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); } #region Pooya public (long Id, UserType userType) GetUserTypeWithId() { if (!IsAuthenticated()) return (0, UserType.Anonymous); var claims = _contextAccessor.HttpContext.User.Claims.ToList(); var subAccountId = long.Parse(claims.FirstOrDefault(x => x.Type == "SubAccountId")?.Value ?? "0"); if (subAccountId > 0) return (subAccountId, UserType.SubAccount); var id = long.Parse(_contextAccessor.HttpContext.User.Claims.First(x => x.Type == "AccountId")?.Value); if (claims.FirstOrDefault(x => x.Type == "AdminAreaPermission")?.Value == "true") return (id, UserType.Admin); return (id, UserType.Client); } #endregion }