| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Security.Claims; |
| | 5 | | using System.Text; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | | using Microsoft.EntityFrameworkCore; |
| | 8 | | using MUNity.Base; |
| | 9 | | using MUNity.Database.Context; |
| | 10 | | using MUNity.Database.Models.Conference; |
| | 11 | | using MUNity.Database.Models.Conference.Roles; |
| | 12 | | using MUNity.Schema.Conference; |
| | 13 | | using MUNity.Schema.Extensions; |
| | 14 | |
|
| | 15 | | namespace MUNity.Services |
| | 16 | | { |
| | 17 | | public class ConferenceRoleService |
| | 18 | | { |
| | 19 | | private readonly MunityContext _context; |
| | 20 | |
|
| | 21 | | private readonly UserConferenceAuthService _authService; |
| | 22 | |
|
| | 23 | | public async Task<CreateTeamRoleGroupResponse> CreateTeamRoleGroupAsync(CreateTeamRoleGroupRequest request, Clai |
| 0 | 24 | | { |
| 0 | 25 | | var response = new CreateTeamRoleGroupResponse(); |
| 0 | 26 | | var isAllowed = await _authService.IsUserAllowedToEditTeam(request.ConferenceId, claim); |
| 0 | 27 | | if (!isAllowed) |
| 0 | 28 | | { |
| 0 | 29 | | response.AddNoPermissionError(); |
| 0 | 30 | | return response; |
| | 31 | | } |
| | 32 | |
|
| 0 | 33 | | var conference = _context.Conferences.FirstOrDefault(n => n.ConferenceId == request.ConferenceId); |
| 0 | 34 | | if (conference == null) |
| 0 | 35 | | response.AddNotFoundError(nameof(request.ConferenceId)); |
| | 36 | |
|
| | 37 | |
|
| 0 | 38 | | if (_context.TeamRoleGroups.Any(n => n.Conference.ConferenceId == request.ConferenceId && n.Name == request. |
| 0 | 39 | | response.AddNotFoundError(nameof(request.GroupName)); |
| | 40 | |
|
| | 41 | |
|
| 0 | 42 | | if (_context.TeamRoleGroups.Any(n => n.Conference.ConferenceId == request.ConferenceId && n.FullName == requ |
| 0 | 43 | | response.AddNotFoundError(nameof(request.GroupFullName)); |
| | 44 | |
|
| | 45 | |
|
| 0 | 46 | | if (_context.TeamRoleGroups.Any(n => n.Conference.ConferenceId == request.ConferenceId && n.TeamRoleGroupSho |
| 0 | 47 | | response.AddNotFoundError(nameof(request.GroupShort)); |
| | 48 | |
|
| | 49 | |
|
| 0 | 50 | | if (response.HasError) |
| 0 | 51 | | return response; |
| | 52 | |
|
| 0 | 53 | | var group = new TeamRoleGroup() |
| 0 | 54 | | { |
| 0 | 55 | | Conference = conference, |
| 0 | 56 | | FullName = request.GroupFullName, |
| 0 | 57 | | GroupLevel = request.GroupLevel, |
| 0 | 58 | | Name = request.GroupName, |
| 0 | 59 | | TeamRoleGroupShort = request.GroupShort |
| 0 | 60 | | }; |
| | 61 | |
|
| 0 | 62 | | _context.TeamRoleGroups.Add(group); |
| 0 | 63 | | await _context.SaveChangesAsync(); |
| 0 | 64 | | response.CreatedGroupId = group.TeamRoleGroupId; |
| 0 | 65 | | return response; |
| 0 | 66 | | } |
| | 67 | |
|
| | 68 | | public async Task<CreateTeamRoleResponse> CreateTeamRoleAsync(CreateTeamRoleRequest request, ClaimsPrincipal cla |
| 0 | 69 | | { |
| 0 | 70 | | var response = new CreateTeamRoleResponse(); |
| 0 | 71 | | var group = _context.TeamRoleGroups.Include(n => n.Conference) |
| 0 | 72 | | .FirstOrDefault(n => n.TeamRoleGroupId == request.RoleGroupId); |
| | 73 | |
|
| 0 | 74 | | if (group == null) |
| 0 | 75 | | { |
| 0 | 76 | | response.AddInvalidDataError("The given group was not found", nameof(request.RoleGroupId)); |
| 0 | 77 | | return response; |
| | 78 | | } |
| | 79 | |
|
| 0 | 80 | | var isAllowed = await _authService.IsUserAllowedToEditTeam(group.Conference.ConferenceId, claim); |
| 0 | 81 | | if (!isAllowed) |
| 0 | 82 | | { |
| 0 | 83 | | response.AddNoPermissionError("You don't have permission to create a Team role"); |
| 0 | 84 | | return response; |
| | 85 | | } |
| | 86 | |
|
| 0 | 87 | | ConferenceTeamRole parentRole = null; |
| 0 | 88 | | if (request.ParentRoleId != -1) |
| 0 | 89 | | { |
| | 90 | | // We are also checking for the ConferenceId to make sure that the given Parent role is in |
| | 91 | | // the same conference! |
| 0 | 92 | | parentRole = _context.ConferenceTeamRoles.FirstOrDefault(n => |
| 0 | 93 | | n.RoleId == request.ParentRoleId && |
| 0 | 94 | | n.Conference.ConferenceId == group.Conference.ConferenceId); |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | |
|
| 0 | 98 | | var role = new ConferenceTeamRole() |
| 0 | 99 | | { |
| 0 | 100 | | Conference = group.Conference, |
| 0 | 101 | | ParentTeamRole = parentRole, |
| 0 | 102 | | RoleName = request.RoleName, |
| 0 | 103 | | RoleShort = request.RoleShort, |
| 0 | 104 | | RoleFullName = request.RoleFullName, |
| 0 | 105 | | TeamRoleGroup = group, |
| 0 | 106 | | TeamRoleLevel = 0 |
| 0 | 107 | | }; |
| 0 | 108 | | _context.ConferenceTeamRoles.Add(role); |
| 0 | 109 | | await _context.SaveChangesAsync(); |
| 0 | 110 | | response.RoleId = role.RoleId; |
| | 111 | |
|
| 0 | 112 | | return response; |
| 0 | 113 | | } |
| | 114 | |
|
| | 115 | |
|
| | 116 | | public async Task<bool> RemoveDelegateRole(int roleId, ClaimsPrincipal claim) |
| 0 | 117 | | { |
| 0 | 118 | | var role = _context.Delegates |
| 0 | 119 | | .Include(n => n.Conference) |
| 0 | 120 | | .FirstOrDefault(n => n.RoleId == roleId); |
| | 121 | |
|
| 0 | 122 | | if (role == null) |
| 0 | 123 | | return false; |
| | 124 | |
|
| 0 | 125 | | var isAllowed = await _authService.IsUserAllowedToEditConference(role.Conference.ConferenceId, claim); |
| 0 | 126 | | if (!isAllowed) |
| 0 | 127 | | return false; |
| | 128 | |
|
| 0 | 129 | | _context.Delegates.Remove(role); |
| 0 | 130 | | await _context.SaveChangesAsync(); |
| 0 | 131 | | return true; |
| 0 | 132 | | } |
| | 133 | |
|
| | 134 | | public async Task<bool> RemoveTeamRole(int roleId, ClaimsPrincipal claim) |
| 0 | 135 | | { |
| 0 | 136 | | var role = _context.ConferenceTeamRoles |
| 0 | 137 | | .Include(n => n.Conference) |
| 0 | 138 | | .FirstOrDefault(n => n.RoleId == roleId); |
| | 139 | |
|
| 0 | 140 | | if (role == null) |
| 0 | 141 | | return false; |
| | 142 | |
|
| 0 | 143 | | var isAllowed = await _authService.IsUserAllowedToEditTeam(role.Conference.ConferenceId, claim); |
| 0 | 144 | | if (!isAllowed) |
| 0 | 145 | | return false; |
| | 146 | |
|
| 0 | 147 | | _context.ConferenceTeamRoles.Remove(role); |
| 0 | 148 | | await _context.SaveChangesAsync(); |
| 0 | 149 | | return true; |
| 0 | 150 | | } |
| | 151 | |
|
| | 152 | | public async Task<bool> SetDelegateRoleApplicationState(int roleId, EApplicationStates state, ClaimsPrincipal cl |
| 0 | 153 | | { |
| 0 | 154 | | var roleWithConference = _context.Delegates |
| 0 | 155 | | .Include(n => n.Conference) |
| 0 | 156 | | .FirstOrDefault(n => n.RoleId == roleId); |
| | 157 | |
|
| 0 | 158 | | if (roleWithConference?.Conference == null) |
| 0 | 159 | | return false; |
| | 160 | |
|
| 0 | 161 | | var isAllowed = await _authService.IsUserAllowedToEditConference(roleWithConference.Conference.ConferenceId, |
| 0 | 162 | | if (!isAllowed) |
| 0 | 163 | | return false; |
| | 164 | |
|
| 0 | 165 | | roleWithConference.ApplicationState = state; |
| 0 | 166 | | _context.SaveChanges(); |
| 0 | 167 | | return true; |
| 0 | 168 | | } |
| | 169 | |
|
| | 170 | | public async Task<bool> SetDelegateRoleApplicationState(int roleId, string state, ClaimsPrincipal claim) |
| 0 | 171 | | { |
| 0 | 172 | | if (state == EApplicationStates.Closed.ToString() || state.ToLower() == "closed") |
| 0 | 173 | | { |
| 0 | 174 | | return await SetDelegateRoleApplicationState(roleId, EApplicationStates.Closed, claim); |
| | 175 | | } |
| 0 | 176 | | else if (state == EApplicationStates.Custom.ToString() || state.ToLower() == "custom") |
| 0 | 177 | | { |
| 0 | 178 | | return await SetDelegateRoleApplicationState(roleId, EApplicationStates.Custom, claim); |
| | 179 | | } |
| 0 | 180 | | else if (state == EApplicationStates.DelegationApplication.ToString() || state.ToLower() == "delegationappli |
| 0 | 181 | | { |
| 0 | 182 | | return await SetDelegateRoleApplicationState(roleId, EApplicationStates.DelegationApplication, claim); |
| | 183 | | } |
| 0 | 184 | | else if (state == EApplicationStates.DirectApplication.ToString() || state.ToLower() == "directapplication" |
| 0 | 185 | | { |
| 0 | 186 | | return await SetDelegateRoleApplicationState(roleId, EApplicationStates.DirectApplication, claim); |
| | 187 | | } |
| | 188 | | else |
| 0 | 189 | | { |
| 0 | 190 | | throw new NotSupportedException($"{state} is not a known Application Type that can be read by string."); |
| | 191 | | } |
| 0 | 192 | | } |
| | 193 | |
|
| | 194 | | public async Task<List<ManageDelegationRoleInfo>> GetRoleInfos(string conferenceId, ClaimsPrincipal claim) |
| 0 | 195 | | { |
| 0 | 196 | | var isAllowed = await _authService.IsUserAllowedToEditConference(conferenceId, claim); |
| 0 | 197 | | if (!isAllowed) |
| 0 | 198 | | return null; |
| | 199 | |
|
| 0 | 200 | | return _context.Delegates.Where(n => n.Conference.ConferenceId == conferenceId) |
| 0 | 201 | | .Select(role => new ManageDelegationRoleInfo() |
| 0 | 202 | | { |
| 0 | 203 | | ApplicationState = role.ApplicationState, |
| 0 | 204 | | HasParicipant = role.Participations.Any(), |
| 0 | 205 | | RoleCommitteeId = role.Committee.CommitteeId, |
| 0 | 206 | | RoleCommitteeName = role.Committee.Name, |
| 0 | 207 | | RoleId = role.RoleId, |
| 0 | 208 | | RoleName = role.RoleName, |
| 0 | 209 | | Subtype = role.DelegateType, |
| 0 | 210 | | DelegationId = role.Delegation.DelegationId, |
| 0 | 211 | | DelegationName = role.Delegation.Name |
| 0 | 212 | | }).ToList(); |
| 0 | 213 | | } |
| | 214 | |
|
| | 215 | | public async Task<List<TeamMemberInfo>> GetTeamMembers(string conferenceId) |
| 0 | 216 | | { |
| 0 | 217 | | return await _context.Participations.Where(n => n.Role is ConferenceTeamRole && n.Role.Conference.Conference |
| 0 | 218 | | .Select(n => new TeamMemberInfo() |
| 0 | 219 | | { |
| 0 | 220 | | UserName = n.User.UserName, |
| 0 | 221 | | DisplayName = n.User.GetDisplayNamePublic, |
| 0 | 222 | | RoleName = n.Role.RoleName |
| 0 | 223 | | }).ToListAsync(); |
| 0 | 224 | | } |
| | 225 | |
|
| 0 | 226 | | public ConferenceRoleService(MunityContext context, UserConferenceAuthService authService) |
| 0 | 227 | | { |
| 0 | 228 | | _context = context; |
| 0 | 229 | | _authService = authService; |
| 0 | 230 | | } |
| | 231 | | } |
| | 232 | | } |