using Infrastructure.Data; using Infrastructure.Web; using IoT.Shared.Application.Domain.Entities; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Security.Claims; namespace Platform { public class UserService : IUserService { private readonly IRepository _organUserRepo; public UserService(IRepository organUserRepo) { this._organUserRepo = organUserRepo; } public List GetOrgans(string userName) { var list = this._organUserRepo.ReadOnlyTable().Where(o => o.User.UserName == userName) .Select(o => o.OrganId.ToString()) .ToList(); return list; } public List GetRoles(string userName, string organId) { var currentOrganId = Guid.Parse(organId); var organUser = this._organUserRepo.ReadOnlyTable() .Where(o => o.User.UserName == userName) .Where(o => o.OrganId == currentOrganId) .Include(o => o.User) .Include(o => o.UserRoles).ThenInclude(o => o.OrganRole).ThenInclude(o => o.RolePermissions).ThenInclude(o => o.Permission) .FirstOrDefault(); List list = null; if (organUser != null) { list = organUser.UserRoles .Select(o => o.OrganRole) .SelectMany(o => o.RolePermissions) .Select(o => o.Permission.Number) .Distinct() .ToList() .Select(o => new Claim(ClaimTypes.Role, o)) .ToList(); list.Add(new Claim(ClaimTypes.GivenName, organUser.User.NickName)); if(!string.IsNullOrEmpty(organUser.User.Avatar)) { list.Add(new Claim("Avatar", organUser.User.Avatar)); } } return list; } } }