| | | 1 | | using System.Reflection; |
| | | 2 | | |
| | | 3 | | namespace Syki.Back.Auth.Permissions; |
| | | 4 | | |
| | | 5 | | public static class SykiPermissions |
| | | 6 | | { |
| | | 7 | | // Identity |
| | 2 | 8 | | public static readonly SykiPermission 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 SykiPermission 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 SykiPermission 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 SykiPermission 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 SykiPermission 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 SykiPermission 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 SykiPermission 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 SykiPermission 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 SykiPermission 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 SykiPermission 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 SykiPermission 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 SykiPermission 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 SykiPermission 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 | | |
| | 2 | 122 | | public static readonly List<PermissionGroup> Groups = []; |
| | 2 | 123 | | public static readonly List<SykiPermission> Permissions = []; |
| | 2 | 124 | | private static readonly Dictionary<int, SykiPermission> ById = []; |
| | | 125 | | static SykiPermissions() |
| | | 126 | | { |
| | 2 | 127 | | Groups = Enum.GetValues<PermissionGroup>().ToList(); |
| | | 128 | | |
| | 2 | 129 | | Permissions = typeof(SykiPermissions) |
| | 2 | 130 | | .GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) |
| | 30 | 131 | | .Where(f => f.FieldType == typeof(SykiPermission)) |
| | 26 | 132 | | .Select(f => (SykiPermission)f.GetValue(null)!) |
| | 26 | 133 | | .OrderBy(f => f.Group) |
| | 26 | 134 | | .ThenBy(f => f.Id) |
| | 2 | 135 | | .ToList(); |
| | | 136 | | |
| | 28 | 137 | | if (!Permissions.Select(x => x.Id).IsAllDistinct()) throw new Exception("Duplicated permission ids!"); |
| | | 138 | | |
| | 28 | 139 | | if (!Permissions.Select(x => x.Name).IsAllDistinct()) throw new Exception("Duplicated permission names!"); |
| | | 140 | | |
| | 28 | 141 | | if (Permissions.Any(x => x.AllowedTypes == null || x.AllowedTypes.Count == 0)) throw new Exception("All permissi |
| | | 142 | | |
| | 28 | 143 | | ById = Permissions.ToDictionary(x => x.Id); |
| | 2 | 144 | | } |
| | | 145 | | |
| | | 146 | | public static bool IsAllowedFor(int permissionId, UserType userType) |
| | | 147 | | { |
| | 2 | 148 | | return ById.TryGetValue(permissionId, out var p) && p.AllowedTypes.Contains(userType); |
| | | 149 | | } |
| | | 150 | | } |