| | 1 | | using Microsoft.AspNetCore.Components.Authorization; |
| | 2 | | using Microsoft.AspNetCore.Identity; |
| | 3 | | using MUNity.Database.Context; |
| | 4 | | using MUNity.Database.Models.Conference.Roles; |
| | 5 | | using MUNity.Database.Models.User; |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Linq; |
| | 9 | | using System.Security.Claims; |
| | 10 | | using System.Text; |
| | 11 | | using System.Threading.Tasks; |
| | 12 | |
|
| | 13 | | namespace MUNity.Services |
| | 14 | | { |
| | 15 | | public class UserConferenceAuthService |
| | 16 | | { |
| | 17 | | private MunityContext context; |
| | 18 | |
|
| | 19 | | private UserManager<MunityUser> userManager; |
| | 20 | |
|
| | 21 | | public bool IsUserAllowedToEditTeam(string conferenceId, string username) |
| 0 | 22 | | { |
| 0 | 23 | | var isCreator = context.Conferences.Any(n => n.ConferenceId == conferenceId && n.CreationUser.UserName == us |
| 0 | 24 | | if (isCreator) |
| 0 | 25 | | return true; |
| | 26 | |
|
| 0 | 27 | | var isAllowedTeamMember = context.Participations.Any(n => n.User.UserName == username && n.Role.Conference.C |
| 0 | 28 | | return isAllowedTeamMember; |
| 0 | 29 | | } |
| | 30 | |
|
| | 31 | | public async Task<bool> IsUserAllowedToEditTeam(string conferenceId, ClaimsPrincipal claim) |
| 0 | 32 | | { |
| 0 | 33 | | var user = await userManager.GetUserAsync(claim); |
| 0 | 34 | | if (user == null) |
| 0 | 35 | | return false; |
| 0 | 36 | | return IsUserAllowedToEditTeam(conferenceId, user.UserName); |
| 0 | 37 | | } |
| | 38 | |
|
| | 39 | | public bool IsUserAllowedToEditConference(string conferenceId, string username) |
| 0 | 40 | | { |
| 0 | 41 | | var isCreator = context.Conferences.Any(n => n.ConferenceId == conferenceId && n.CreationUser.UserName == us |
| 0 | 42 | | if (isCreator) |
| 0 | 43 | | return true; |
| | 44 | |
|
| 0 | 45 | | var isAllowedTeamMember = context.Participations.Any(n => n.User.UserName == username && n.Role.Conference.C |
| 0 | 46 | | return isAllowedTeamMember; |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | public async Task<bool> IsUserAllowedToEditConference(string conferenceId, ClaimsPrincipal claim) |
| 0 | 50 | | { |
| 0 | 51 | | var user = await userManager.GetUserAsync(claim); |
| 0 | 52 | | if (user == null) |
| 0 | 53 | | return false; |
| | 54 | |
|
| 0 | 55 | | return IsUserAllowedToEditConference(conferenceId, user.UserName); |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | public async Task<bool> IsUserAllowedToEditConference(string conferenceId, Task<AuthenticationState> authStateTa |
| 0 | 59 | | { |
| 0 | 60 | | return await IsUserAllowedToEditConference(conferenceId, (await authStateTask)?.User); |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | public async Task<bool> IsUserTeamMember(string conferenceId, ClaimsPrincipal claim) |
| 0 | 64 | | { |
| 0 | 65 | | var user = await userManager.GetUserAsync(claim); |
| 0 | 66 | | if (user == null) |
| 0 | 67 | | return false; |
| | 68 | |
|
| 0 | 69 | | return context.Participations.Any(n => n.Role is ConferenceTeamRole && n.User == user && n.Role.Conference.C |
| 0 | 70 | | } |
| | 71 | |
|
| | 72 | | public Task<MunityUser> GetUserAsync(ClaimsPrincipal claim) |
| 0 | 73 | | { |
| 0 | 74 | | return userManager.GetUserAsync(claim); |
| 0 | 75 | | } |
| | 76 | |
|
| 0 | 77 | | public UserConferenceAuthService(MunityContext context, UserManager<MunityUser> userManager) |
| 0 | 78 | | { |
| 0 | 79 | | this.context = context; |
| 0 | 80 | | this.userManager = userManager; |
| 0 | 81 | | } |
| | 82 | | } |
| | 83 | | } |