public CommonResult<ClaimsData> GetClaimsForUser(string userName) { using (var context = ContextFactory.GetDbContext(_dbConfiguration)) { var user = context.Users.FirstOrDefault(x => x.UserName == userName); if (user == null) { return CommonResult<ClaimsData>.Failure("Użytkownik nie istnieje."); } var claims = context.UserClaims .Where(x => x.UserId == user.Id) .Select(x => new Claim(x.ClaimType, x.ClaimValue)); var claimsData = new ClaimsData(); claimsData.ControllerAdministrationController = CheckClaim(claims, ClaimTypeEnum.Controller, ClaimValueEnum.Controller_AdministrationController); claimsData.ControllerHomeController = CheckClaim(claims, ClaimTypeEnum.Controller, ClaimValueEnum.Controller_HomeController); claimsData.ControllerNewsController = CheckClaim(claims, ClaimTypeEnum.Controller, ClaimValueEnum.Controller_NewsController); claimsData.ControllerTasksController = CheckClaim(claims, ClaimTypeEnum.Controller, ClaimValueEnum.Controller_TasksController); claimsData.ControllerGroupsController = CheckClaim(claims, ClaimTypeEnum.Controller, ClaimValueEnum.Controller_GroupsController); claimsData.TaskCreate = CheckClaim(claims, ClaimTypeEnum.Task, ClaimValueEnum.Task_Create); claimsData.NewsCreate = CheckClaim(claims, ClaimTypeEnum.News, ClaimValueEnum.News_Create); claimsData.GroupCreate = CheckClaim(claims, ClaimTypeEnum.Group, ClaimValueEnum.Group_Create); return CommonResult<ClaimsData>.Success(claimsData); } }
public CommonResult UpdateClaims(string userName, ClaimsData claims) { var propertiesInfo = claims.GetType().GetProperties(); foreach (var property in propertiesInfo) { var claimMapping = (ClaimMappingAttribute)property.GetCustomAttributes(typeof(ClaimMappingAttribute), false).First(); if ((bool)property.GetValue(claims) == true) { AddClaim(userName, claimMapping.ClaimType, claimMapping.ClaimValue); } else { RemoveClaim(userName, claimMapping.ClaimType, claimMapping.ClaimValue); } } return CommonResult.Success(); }