oc-front/lib/core/services/perms_service.dart
2024-11-19 15:06:22 +01:00

105 lines
3.7 KiB
Dart

import 'dart:convert';
enum Perms {
SEARCH_INTERNAL,// ignore: constant_identifier_names
SEARCH_EXTERNAL, // ignore: constant_identifier_names
WORKSPACE_SHARE,// ignore: constant_identifier_names
WORKSPACE_UNSHARE,// ignore: constant_identifier_names
WORKFLOW_CREATE, // ignore: constant_identifier_names
WORKFLOW_EDIT, // ignore: constant_identifier_names
WORKFLOW_DELETE, // ignore: constant_identifier_names
WORKFLOW_BOOKING, // ignore: constant_identifier_names
WORKFLOW_SHARE, // ignore: constant_identifier_names
WORKFLOW_UNSHARE, // ignore: constant_identifier_names
PEER_SHARE, // ignore: constant_identifier_names
PEER_UNSHARE, // ignore: constant_identifier_names
COLLABORATIVE_AREA_CREATE, // ignore: constant_identifier_names
COLLABORATIVE_AREA_EDIT, // ignore: constant_identifier_names
COLLABORATIVE_AREA_DELETE, // ignore: constant_identifier_names
}
Map<Perms, String> perms = {
Perms.SEARCH_INTERNAL: 'GET__catalog_compute_search_search'.toUpperCase(),
Perms.SEARCH_EXTERNAL: 'Search External'.toUpperCase(),
Perms.WORKSPACE_SHARE: 'POST__shared_collaborative_area_id_workspace_id2'.toUpperCase(),
Perms.WORKFLOW_CREATE: 'POST__workflow_'.toUpperCase(),
Perms.WORKFLOW_UNSHARE: 'DELETE__shared_collaborative_area_id_workflow_id2'.toUpperCase(),
Perms.PEER_SHARE: 'POST__shared_collaborative_area_id_peer_id2'.toUpperCase(),
Perms.PEER_UNSHARE: 'DELETE__shared_collaborative_area_id_peer_id2'.toUpperCase(),
Perms.COLLABORATIVE_AREA_CREATE: 'POST__shared_collaborative_area_'.toUpperCase(),
Perms.COLLABORATIVE_AREA_EDIT: 'PUT__shared_collaborative_area_id'.toUpperCase(),
Perms.COLLABORATIVE_AREA_DELETE: 'DELETE__shared_collaborative_area_id'.toUpperCase(),
Perms.WORKSPACE_UNSHARE: 'DELETE__shared_collaborative_area_id_workspace_id2'.toUpperCase(),
Perms.WORKFLOW_EDIT: 'PUT__workflow_id'.toUpperCase(),
Perms.WORKFLOW_DELETE: 'DELETE__workflow_id'.toUpperCase(),
Perms.WORKFLOW_BOOKING: 'POST__datacenter_booking_'.toUpperCase(),
Perms.WORKFLOW_SHARE: 'POST__shared_collaborative_area_id_workflow_id2'.toUpperCase(),
};
class PermsService {
static final Map<Perms, bool> _perms = {
Perms.SEARCH_INTERNAL: true,
Perms.SEARCH_EXTERNAL: true,
Perms.WORKSPACE_SHARE: true,
Perms.WORKSPACE_UNSHARE: true,
Perms.WORKFLOW_CREATE: true,
Perms.WORKFLOW_EDIT: true,
Perms.WORKFLOW_DELETE: true,
Perms.WORKFLOW_BOOKING: true,
Perms.WORKFLOW_SHARE: true,
Perms.WORKFLOW_UNSHARE: true,
Perms.PEER_SHARE: true,
Perms.PEER_UNSHARE: true,
Perms.COLLABORATIVE_AREA_CREATE: true,
Perms.COLLABORATIVE_AREA_EDIT: true,
Perms.COLLABORATIVE_AREA_DELETE: true,
};
static final PermsService _instance = PermsService._internal();
factory PermsService() => _instance;
PermsService._internal();
/* should decode claims such as in oc-auth */
static Future<void> init(String token ) async {
/* var claims = token.split(".").last;
var decoded = base64.decode(claims);
String foo = utf8.decode(decoded);
var what = json.decode(foo);
try {
what = what["session"]["access_token"] as Map<String, dynamic>;
for (var w in perms.values) {
if (what.keys.contains(w)) {
print("CONTAINS");
} else {
for (var y in what.keys) {
print("${w}, ${y} ${what.keys.contains(w)}");
}
}
}
} catch (e) {
print("THERE");
}*/
_perms.forEach((key, value) {
_perms[key] = true;
});
}
static void clear() {
_perms.forEach((key, value) {
_perms[key] = false;
});
}
static bool getPerm(Perms perm) {
return _perms[perm] ?? false;
}
static void setPerm(Perms perm, bool value) {
_perms[perm] = value;
}
}