init
This commit is contained in:
commit
e124a47765
19374 changed files with 9806149 additions and 0 deletions
BIN
Kreta.WebApi/Ellenorzo/Kreta.Ellenorzo.WebApi/VN/Common/.DS_Store
vendored
Normal file
BIN
Kreta.WebApi/Ellenorzo/Kreta.Ellenorzo.WebApi/VN/Common/.DS_Store
vendored
Normal file
Binary file not shown.
|
@ -0,0 +1,59 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Kreta.Ellenorzo.WebApi.VN.Logic
|
||||
{
|
||||
/// <summary>
|
||||
/// Author: Kovács Kornél (DevKornél) Created On: 2019.06.
|
||||
/// </summary>
|
||||
internal static class EnumLogic
|
||||
{
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
|
||||
public static IEnumerable<(int Id, string Nev, string Leiras)> GetEnumWithDisplayNameAttribute(Type enumType)
|
||||
{
|
||||
foreach (object item in Enum.GetValues(enumType))
|
||||
{
|
||||
(int Id, string Nev, string Leiras) returnObject = ((int)item, item.ToString(), null);
|
||||
FieldInfo fieldInfo = item.GetType().GetField(returnObject.Nev);
|
||||
|
||||
if (fieldInfo != null)
|
||||
{
|
||||
DisplayAttribute attributes = (DisplayAttribute)fieldInfo.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault();
|
||||
|
||||
if (attributes != null)
|
||||
{
|
||||
returnObject.Leiras = attributes.GetName();
|
||||
}
|
||||
}
|
||||
|
||||
yield return returnObject;
|
||||
}
|
||||
}
|
||||
|
||||
public static string GetDisplayNameAttribute<T>(T enumValue)
|
||||
{
|
||||
string nev = enumValue.ToString();
|
||||
if (string.IsNullOrWhiteSpace(nev))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
FieldInfo fieldInfo = typeof(T).GetField(nev);
|
||||
|
||||
if (fieldInfo != null)
|
||||
{
|
||||
DisplayAttribute attributes = (DisplayAttribute)fieldInfo.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault();
|
||||
|
||||
if (attributes != null)
|
||||
{
|
||||
return attributes.GetName();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,99 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Security.Claims;
|
||||
using System.Web;
|
||||
using Kreta.Ellenorzo.Domain.VN.Common;
|
||||
using Kreta.Ellenorzo.Enums;
|
||||
|
||||
namespace Kreta.Ellenorzo.WebApi.VN.Logic
|
||||
{
|
||||
internal static class FelhasznaloLogic
|
||||
{
|
||||
public static MobileUser GetFelhasznalo()
|
||||
{
|
||||
return HttpContext.Current.Items.Contains("MobileUser")
|
||||
? (MobileUser)HttpContext.Current.Items["MobileUser"]
|
||||
: new MobileUser(GetInstituteCode(), GetStudentId(), GetUserName(), GetTutelaryId(), GetRoles(), GetSchoolYearId(), GetApiSecurity(), GetUserIdpUniqueId(), GetStudentIdpUniqueId(), GetInstituteUniqueId());
|
||||
}
|
||||
|
||||
private static string GetInstituteCode()
|
||||
{
|
||||
return GetClaims("kreta:institute_code").Single();
|
||||
}
|
||||
|
||||
private static Guid GetInstituteUniqueId()
|
||||
{
|
||||
return new Guid(GetClaims("kreta:institute_unique_id").Single());
|
||||
}
|
||||
|
||||
private static ApiSecurity GetApiSecurity()
|
||||
{
|
||||
if (bool.Parse(ConfigurationManager.AppSettings["IsApiSecurityEnabled"]))
|
||||
{
|
||||
var header = ((HttpRequestMessage)HttpContext.Current.Items["MS_HttpRequestMessage"]).Headers;
|
||||
return new ApiSecurity(header.Authorization.Parameter, header.First(x => x.Key == "s").Value.First());
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static int GetInstituteUserId()
|
||||
{
|
||||
var claim = GetClaims("kreta:institute_user_id").Single();
|
||||
return int.Parse(claim);
|
||||
}
|
||||
|
||||
private static int GetSchoolYearId()
|
||||
{
|
||||
var claim = GetClaims("kreta:school_year_id").Single();
|
||||
return int.Parse(claim);
|
||||
}
|
||||
|
||||
private static int? GetTutelaryId()
|
||||
=> HasRole(FelhasznaloSzerepkor.Gondviselo) ? GetInstituteUserId() : (int?)null;
|
||||
|
||||
private static int GetStudentId()
|
||||
=> HasRole(FelhasznaloSzerepkor.Gondviselo) ? int.Parse(GetClaims("kreta:student_id").Single()) : GetInstituteUserId();
|
||||
|
||||
private static Guid GetUserIdpUniqueId()
|
||||
{
|
||||
return new Guid(GetClaims("kreta:institute_user_idp_unique_id").Single());
|
||||
}
|
||||
|
||||
private static Guid GetStudentIdpUniqueId()
|
||||
=> HasRole(FelhasznaloSzerepkor.Gondviselo) ? new Guid(GetClaims("kreta:student_idp_unique_id").Single()) : GetUserIdpUniqueId();
|
||||
|
||||
private static string GetUserName()
|
||||
{
|
||||
return GetClaims("kreta:user_name").Single();
|
||||
}
|
||||
|
||||
private static IEnumerable<FelhasznaloSzerepkor> GetRoles()
|
||||
{
|
||||
var result = new List<FelhasznaloSzerepkor>();
|
||||
|
||||
var roles = GetClaims(ClaimTypes.Role);
|
||||
foreach (var role in roles)
|
||||
{
|
||||
if (Enum.TryParse(role, out FelhasznaloSzerepkor enumValue))
|
||||
{
|
||||
result.Add(enumValue);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static IEnumerable<string> GetClaims(string type)
|
||||
{
|
||||
return ClaimsPrincipal.Current.Claims.Where(x => x.Type == type).Select(x => x.Value);
|
||||
}
|
||||
|
||||
private static bool HasRole(FelhasznaloSzerepkor felhasznaloSzerepkor) => GetRoles().Count(x => x == felhasznaloSzerepkor) == 1;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue