using System; using System.Collections.Generic; using System.Linq; using Kreta.Ellenorzo.Enums; namespace Kreta.Ellenorzo.Domain.VN.Common { public class MobileUser { private const int MinimumIdentifierValue = 1; public MobileUser(string instituteCode, int userId, string userName, int? tutelaryId, IEnumerable roles, int schoolYearId, ApiSecurity apiSecurity, Guid userIdpUniqueId, Guid studentIdpUniqueId, Guid instituteUniqueId) { ApiSecurity = apiSecurity; if (string.IsNullOrWhiteSpace(instituteCode)) { throw new ArgumentException($"{nameof(instituteCode)} cannot be null or whitespace"); } InstituteCode = instituteCode; if (userId < MinimumIdentifierValue) { throw new ArgumentException($"{nameof(userId)} must be greater or equal to {MinimumIdentifierValue}"); } UserId = userId; if (string.IsNullOrWhiteSpace(userName)) { throw new ArgumentException($"{nameof(userName)} cannot be null or whitespace"); } UserName = userName; if (roles == null) { throw new ArgumentNullException(nameof(roles)); } if (!roles.Any()) { throw new ArgumentException($"User \"{instituteCode}/{userId}\" must have at least one role"); } Roles = roles; if (tutelaryId != null) { if (tutelaryId < MinimumIdentifierValue) { throw new ArgumentException($"{nameof(tutelaryId)} must be greater or equal to {MinimumIdentifierValue}"); } if (!roles.Contains(FelhasznaloSzerepkor.Gondviselo)) { throw new ArgumentException($"{nameof(roles)} must contain {FelhasznaloSzerepkor.Gondviselo} because {nameof(tutelaryId)} is not null"); } } TutelaryId = tutelaryId; if (schoolYearId < MinimumIdentifierValue) { throw new ArgumentException($"{nameof(schoolYearId)} must be greater or equal to {MinimumIdentifierValue}"); } SchoolYearId = schoolYearId; UserIdpUniqueId = userIdpUniqueId; StudentIdpUniqueId = studentIdpUniqueId; InstituteUniqueId = instituteUniqueId; } public int ActualUserId => TutelaryId ?? UserId; public string InstituteCode { get; } public ApiSecurity ApiSecurity { get; } public int SchoolYearId { get; } public IEnumerable Roles { get; } public int? TutelaryId { get; } public int UserId { get; } public string UserName { get; } public Guid UserIdpUniqueId { get; } public Guid StudentIdpUniqueId { get; } public Guid InstituteUniqueId { get; } } }