| | | 1 | | using System.Reflection; |
| | | 2 | | |
| | | 3 | | namespace Estud.Back.Auth.Permissions; |
| | | 4 | | |
| | | 5 | | public static class EstudPermissions |
| | | 6 | | { |
| | | 7 | | // Identity |
| | 2 | 8 | | public static readonly EstudPermission ManageRoles = new( |
| | 2 | 9 | | PermissionGroup.Identity, |
| | 2 | 10 | | 000, |
| | 2 | 11 | | "Gerenciar perfis de acesso.", |
| | 2 | 12 | | "Criar, editar e deletar perfis de acesso.", |
| | 2 | 13 | | [UserType.Manager] |
| | 2 | 14 | | ); |
| | 2 | 15 | | public static readonly EstudPermission ManageSso = new( |
| | 2 | 16 | | PermissionGroup.Identity, |
| | 2 | 17 | | 001, |
| | 2 | 18 | | "Gerenciar SSO.", |
| | 2 | 19 | | "Configurar Single Sign-On (SSO) para a instituição.", |
| | 2 | 20 | | [UserType.Manager] |
| | 2 | 21 | | ); |
| | | 22 | | |
| | | 23 | | // Users |
| | 2 | 24 | | public static readonly EstudPermission ManageUsers = new( |
| | 2 | 25 | | PermissionGroup.Users, |
| | 2 | 26 | | 100, |
| | 2 | 27 | | "Gerenciar usuários.", |
| | 2 | 28 | | "Criar, editar e deletar usuários.", |
| | 2 | 29 | | [UserType.Manager] |
| | 2 | 30 | | ); |
| | | 31 | | |
| | | 32 | | // Campi |
| | 2 | 33 | | public static readonly EstudPermission ManageCampi = new( |
| | 2 | 34 | | PermissionGroup.Campi, |
| | 2 | 35 | | 200, |
| | 2 | 36 | | "Gerenciar campus.", |
| | 2 | 37 | | "Criar e editar campus.", |
| | 2 | 38 | | [UserType.Manager] |
| | 2 | 39 | | ); |
| | | 40 | | |
| | | 41 | | // Disciplines |
| | 2 | 42 | | public static readonly EstudPermission ManageDisciplines = new( |
| | 2 | 43 | | PermissionGroup.Disciplines, |
| | 2 | 44 | | 300, |
| | 2 | 45 | | "Gerenciar disciplinas.", |
| | 2 | 46 | | "Criar e editar disciplinas.", |
| | 2 | 47 | | [UserType.Manager] |
| | 2 | 48 | | ); |
| | | 49 | | |
| | | 50 | | // Courses |
| | 2 | 51 | | public static readonly EstudPermission ManageCourses = new( |
| | 2 | 52 | | PermissionGroup.Courses, |
| | 2 | 53 | | 400, |
| | 2 | 54 | | "Gerenciar cursos.", |
| | 2 | 55 | | "Criar e editar cursos.", |
| | 2 | 56 | | [UserType.Manager] |
| | 2 | 57 | | ); |
| | | 58 | | |
| | | 59 | | // Teachers |
| | 2 | 60 | | public static readonly EstudPermission ManageTeachers = new( |
| | 2 | 61 | | PermissionGroup.Teachers, |
| | 2 | 62 | | 500, |
| | 2 | 63 | | "Gerenciar professores.", |
| | 2 | 64 | | "Criar e editar professores.", |
| | 2 | 65 | | [UserType.Manager] |
| | 2 | 66 | | ); |
| | | 67 | | |
| | | 68 | | // Students |
| | 2 | 69 | | public static readonly EstudPermission ManageStudents = new( |
| | 2 | 70 | | PermissionGroup.Students, |
| | 2 | 71 | | 600, |
| | 2 | 72 | | "Gerenciar alunos.", |
| | 2 | 73 | | "Criar e editar alunos.", |
| | 2 | 74 | | [UserType.Manager] |
| | 2 | 75 | | ); |
| | | 76 | | |
| | | 77 | | // Periods |
| | 2 | 78 | | public static readonly EstudPermission ManagePeriods = new( |
| | 2 | 79 | | PermissionGroup.Periods, |
| | 2 | 80 | | 700, |
| | 2 | 81 | | "Gerenciar períodos acadêmicos.", |
| | 2 | 82 | | "Criar e editar períodos acadêmicos.", |
| | 2 | 83 | | [UserType.Manager] |
| | 2 | 84 | | ); |
| | | 85 | | |
| | | 86 | | // CourseCurriculums |
| | 2 | 87 | | public static readonly EstudPermission ManageCourseCurriculums = new( |
| | 2 | 88 | | PermissionGroup.CourseCurriculums, |
| | 2 | 89 | | 800, |
| | 2 | 90 | | "Gerenciar grades curriculares.", |
| | 2 | 91 | | "Criar e editar grades curriculares.", |
| | 2 | 92 | | [UserType.Manager] |
| | 2 | 93 | | ); |
| | | 94 | | |
| | | 95 | | // CourseOfferings |
| | 2 | 96 | | public static readonly EstudPermission ManageCourseOfferings = new( |
| | 2 | 97 | | PermissionGroup.CourseOfferings, |
| | 2 | 98 | | 900, |
| | 2 | 99 | | "Gerenciar ofertas de curso.", |
| | 2 | 100 | | "Criar e editar ofertas de curso.", |
| | 2 | 101 | | [UserType.Manager] |
| | 2 | 102 | | ); |
| | | 103 | | |
| | | 104 | | // Classes |
| | 2 | 105 | | public static readonly EstudPermission ManageClasses = new( |
| | 2 | 106 | | PermissionGroup.Classes, |
| | 2 | 107 | | 1000, |
| | 2 | 108 | | "Gerenciar turmas.", |
| | 2 | 109 | | "Criar e editar turmas.", |
| | 2 | 110 | | [UserType.Manager] |
| | 2 | 111 | | ); |
| | | 112 | | |
| | | 113 | | // Webhooks |
| | 2 | 114 | | public static readonly EstudPermission ManageWebhooks = new( |
| | 2 | 115 | | PermissionGroup.Webhooks, |
| | 2 | 116 | | 1100, |
| | 2 | 117 | | "Gerenciar webhooks.", |
| | 2 | 118 | | "Criar e visualizar inscrições de webhook.", |
| | 2 | 119 | | [UserType.Manager] |
| | 2 | 120 | | ); |
| | | 121 | | |
| | | 122 | | // Notifications |
| | 2 | 123 | | public static readonly EstudPermission ManageNotifications = new( |
| | 2 | 124 | | PermissionGroup.Notifications, |
| | 2 | 125 | | 1200, |
| | 2 | 126 | | "Gerenciar notificações.", |
| | 2 | 127 | | "Criar e visualizar notificações.", |
| | 2 | 128 | | [UserType.Manager] |
| | 2 | 129 | | ); |
| | | 130 | | |
| | | 131 | | // Classrooms |
| | 2 | 132 | | public static readonly EstudPermission ManageClassrooms = new( |
| | 2 | 133 | | PermissionGroup.Classrooms, |
| | 2 | 134 | | 1300, |
| | 2 | 135 | | "Gerenciar salas de aula.", |
| | 2 | 136 | | "Criar e editar salas de aula.", |
| | 2 | 137 | | [UserType.Manager] |
| | 2 | 138 | | ); |
| | | 139 | | |
| | 2 | 140 | | public static readonly List<PermissionGroup> Groups = []; |
| | 2 | 141 | | public static readonly List<EstudPermission> Permissions = []; |
| | 2 | 142 | | private static readonly Dictionary<int, EstudPermission> ById = []; |
| | | 143 | | static EstudPermissions() |
| | | 144 | | { |
| | 2 | 145 | | Groups = Enum.GetValues<PermissionGroup>().ToList(); |
| | | 146 | | |
| | 2 | 147 | | Permissions = typeof(EstudPermissions) |
| | 2 | 148 | | .GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) |
| | 34 | 149 | | .Where(f => f.FieldType == typeof(EstudPermission)) |
| | 30 | 150 | | .Select(f => (EstudPermission)f.GetValue(null)!) |
| | 30 | 151 | | .OrderBy(f => f.Group) |
| | 30 | 152 | | .ThenBy(f => f.Id) |
| | 2 | 153 | | .ToList(); |
| | | 154 | | |
| | 32 | 155 | | if (!Permissions.Select(x => x.Id).IsAllDistinct()) throw new Exception("Duplicated permission ids!"); |
| | | 156 | | |
| | 32 | 157 | | if (!Permissions.Select(x => x.Name).IsAllDistinct()) throw new Exception("Duplicated permission names!"); |
| | | 158 | | |
| | 32 | 159 | | if (Permissions.Any(x => x.AllowedTypes == null || x.AllowedTypes.Count == 0)) throw new Exception("All permissi |
| | | 160 | | |
| | 32 | 161 | | ById = Permissions.ToDictionary(x => x.Id); |
| | 2 | 162 | | } |
| | | 163 | | |
| | | 164 | | public static bool IsAllowedFor(int permissionId, UserType userType) |
| | | 165 | | { |
| | 4 | 166 | | return ById.TryGetValue(permissionId, out var p) && p.AllowedTypes.Contains(userType); |
| | | 167 | | } |
| | | 168 | | } |