|
| 1 | +import Mysql from './mysql.js' |
| 2 | +import Permission from './permission.js' |
| 3 | + |
| 4 | +const RESOURCE_QUERIES = { |
| 5 | + zone: 'SELECT nt_group_id FROM nt_zone WHERE nt_zone_id = ?', |
| 6 | + zonerecord: `SELECT z.nt_group_id FROM nt_zone_record r |
| 7 | + JOIN nt_zone z ON z.nt_zone_id = r.nt_zone_id |
| 8 | + WHERE r.nt_zone_record_id = ?`, |
| 9 | + user: 'SELECT nt_group_id FROM nt_user WHERE nt_user_id = ?', |
| 10 | + group: 'SELECT parent_group_id AS nt_group_id FROM nt_group WHERE nt_group_id = ?', |
| 11 | + nameserver: 'SELECT nt_group_id FROM nt_nameserver WHERE nt_nameserver_id = ?', |
| 12 | +} |
| 13 | + |
| 14 | +const DELEGATE_TYPE = { |
| 15 | + zone: 'ZONE', |
| 16 | + zonerecord: 'ZONERECORD', |
| 17 | + nameserver: 'NAMESERVER', |
| 18 | + group: 'GROUP', |
| 19 | +} |
| 20 | + |
| 21 | +const PERM_FIELDS = [ |
| 22 | + 'group_write', 'group_create', 'group_delete', |
| 23 | + 'zone_write', 'zone_create', 'zone_delegate', 'zone_delete', |
| 24 | + 'zonerecord_write', 'zonerecord_create', 'zonerecord_delegate', 'zonerecord_delete', |
| 25 | + 'user_write', 'user_create', 'user_delete', |
| 26 | + 'nameserver_write', 'nameserver_create', 'nameserver_delete', |
| 27 | +] |
| 28 | + |
| 29 | +class Authz { |
| 30 | + async checkPermission(credentials, resource, action, objectId, opts) { |
| 31 | + const perm = await Permission.getEffective(credentials.user.id) |
| 32 | + if (!perm) return deny(`No permissions found`) |
| 33 | + |
| 34 | + if (action === 'create') { |
| 35 | + if (perm[resource]?.create !== true) { |
| 36 | + return deny(`Not allowed to create new ${resource}`) |
| 37 | + } |
| 38 | + const targetGid = opts?.targetGroupId |
| 39 | + if (targetGid) { |
| 40 | + const inTree = await this.isInGroupTree( |
| 41 | + credentials.group.id, targetGid, |
| 42 | + ) |
| 43 | + if (!inTree) { |
| 44 | + return deny( |
| 45 | + `No Access Allowed to that object` |
| 46 | + + ` (${DELEGATE_TYPE[resource] ?? 'GROUP'} : ${targetGid})`, |
| 47 | + ) |
| 48 | + } |
| 49 | + } |
| 50 | + return allow() |
| 51 | + } |
| 52 | + |
| 53 | + if (resource === 'user' && objectId === credentials.user.id) { |
| 54 | + if (action === 'delete') return deny(`Not allowed to delete self`) |
| 55 | + if (action === 'write') { |
| 56 | + if (perm.self_write !== true) return deny(`Not allowed to modify self`) |
| 57 | + return allow() |
| 58 | + } |
| 59 | + return allow() |
| 60 | + } |
| 61 | + |
| 62 | + if (resource === 'group' && objectId === credentials.group.id) { |
| 63 | + if (action === 'write') return deny(`Not allowed to edit your own group`) |
| 64 | + if (action === 'delete') return deny(`Not allowed to delete your own group`) |
| 65 | + } |
| 66 | + |
| 67 | + if (resource === 'nameserver' && action === 'read') { |
| 68 | + const usable = perm.nameserver?.usable ?? [] |
| 69 | + if (usable.includes(String(objectId))) return allow() |
| 70 | + } |
| 71 | + |
| 72 | + const objGroupId = await this.getObjectGroupId(resource, objectId) |
| 73 | + if (objGroupId === null) { |
| 74 | + return deny(`No Access Allowed to that object (${DELEGATE_TYPE[resource]} : ${objectId})`) |
| 75 | + } |
| 76 | + |
| 77 | + if (await this.isInGroupTree(credentials.group.id, objGroupId)) { |
| 78 | + if (action === 'read') return allow() |
| 79 | + if (perm[resource]?.[action] === true) return allow() |
| 80 | + return deny(`You have no '${action}' permission for ${resource} objects`) |
| 81 | + } |
| 82 | + |
| 83 | + const delegation = await this.getDelegateAccess( |
| 84 | + credentials.group.id, objectId, resource, |
| 85 | + ) |
| 86 | + if (delegation) { |
| 87 | + if (action === 'read') return allow() |
| 88 | + const permField = `perm_${action === 'delegate' ? 'delegate' : action}` |
| 89 | + if (delegation[permField] === 1) return allow() |
| 90 | + return deny(`You have no '${action}' permission for the delegated object`) |
| 91 | + } |
| 92 | + |
| 93 | + return deny( |
| 94 | + `No Access Allowed to that object (${DELEGATE_TYPE[resource]} : ${objectId})`, |
| 95 | + ) |
| 96 | + } |
| 97 | + |
| 98 | + async getObjectGroupId(resource, objectId) { |
| 99 | + const query = RESOURCE_QUERIES[resource] |
| 100 | + if (!query) return null |
| 101 | + |
| 102 | + const rows = await Mysql.execute(query, [objectId]) |
| 103 | + if (rows.length === 0) return null |
| 104 | + |
| 105 | + let gid = rows[0].nt_group_id |
| 106 | + if (resource === 'group' && (gid === 0 || gid === null)) gid = 1 |
| 107 | + return gid |
| 108 | + } |
| 109 | + |
| 110 | + async isInGroupTree(userGroupId, targetGroupId) { |
| 111 | + if (userGroupId === targetGroupId) return true |
| 112 | + |
| 113 | + const rows = await Mysql.execute( |
| 114 | + `SELECT COUNT(*) AS count FROM nt_group_subgroups |
| 115 | + WHERE nt_group_id = ? AND nt_subgroup_id = ?`, |
| 116 | + [userGroupId, targetGroupId], |
| 117 | + ) |
| 118 | + return rows[0].count > 0 |
| 119 | + } |
| 120 | + |
| 121 | + async getDelegateAccess(groupId, objectId, resource) { |
| 122 | + const type = DELEGATE_TYPE[resource] |
| 123 | + if (!type) return null |
| 124 | + |
| 125 | + const rows = await Mysql.execute( |
| 126 | + `SELECT * FROM nt_delegate |
| 127 | + WHERE nt_group_id = ? AND nt_object_id = ? AND nt_object_type = ? AND deleted = 0`, |
| 128 | + [groupId, objectId, type], |
| 129 | + ) |
| 130 | + if (rows.length > 0) return rows[0] |
| 131 | + |
| 132 | + if (resource === 'zonerecord') { |
| 133 | + return this.getZoneRecordPseudoDelegation(groupId, objectId) |
| 134 | + } |
| 135 | + return null |
| 136 | + } |
| 137 | + |
| 138 | + async getZoneRecordPseudoDelegation(groupId, zoneRecordId) { |
| 139 | + const rows = await Mysql.execute( |
| 140 | + `SELECT d.*, 1 AS pseudo FROM nt_delegate d |
| 141 | + JOIN nt_zone_record r ON r.nt_zone_id = d.nt_object_id |
| 142 | + WHERE d.nt_group_id = ? |
| 143 | + AND r.nt_zone_record_id = ? |
| 144 | + AND d.nt_object_type = 'ZONE' |
| 145 | + AND d.deleted = 0`, |
| 146 | + [groupId, zoneRecordId], |
| 147 | + ) |
| 148 | + return rows.length > 0 ? rows[0] : null |
| 149 | + } |
| 150 | + |
| 151 | + capPermissions(userPerm, targetPerms) { |
| 152 | + if (!targetPerms || !userPerm) return targetPerms |
| 153 | + |
| 154 | + const capped = { ...targetPerms } |
| 155 | + for (const field of PERM_FIELDS) { |
| 156 | + if (capped[field] === undefined) continue |
| 157 | + const [resource] = field.split('_', 2) |
| 158 | + const remaining = field.slice(resource.length + 1) |
| 159 | + if (userPerm[resource]?.[remaining] !== true) { |
| 160 | + delete capped[field] |
| 161 | + } |
| 162 | + } |
| 163 | + return capped |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +function allow() { |
| 168 | + return { allowed: true } |
| 169 | +} |
| 170 | + |
| 171 | +function deny(msg) { |
| 172 | + return { allowed: false, code: 404, msg } |
| 173 | +} |
| 174 | + |
| 175 | +export default new Authz() |
0 commit comments