ProtectedAppService.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. const { get_app } = require("../helpers");
  2. const { UserActorType } = require("./auth/Actor");
  3. const { PermissionImplicator, PermissionUtil } = require("./auth/PermissionService");
  4. const BaseService = require("./BaseService");
  5. class ProtectedAppService extends BaseService {
  6. async _init () {
  7. const svc_permission = this.services.get('permission');
  8. // track: object description in comment
  9. // Owner of procted app has implicit permission to access it
  10. svc_permission.register_implicator(PermissionImplicator.create({
  11. matcher: permission => {
  12. return permission.startsWith('app:');
  13. },
  14. checker: async ({ actor, permission }) => {
  15. if ( !(actor.type instanceof UserActorType) ) {
  16. return undefined;
  17. }
  18. const parts = PermissionUtil.split(permission);
  19. if ( parts.length !== 3 ) return undefined;
  20. const [_, uid_part, lvl] = parts;
  21. if ( lvl !== 'access' ) return undefined;
  22. // track: slice a prefix
  23. const uid = uid_part.slice('uid#'.length);
  24. const app = await get_app({ uid });
  25. if ( app.owner_user_id !== actor.type.user.id ) {
  26. return undefined;
  27. }
  28. return {};
  29. },
  30. }));
  31. }
  32. }
  33. module.exports = {
  34. ProtectedAppService,
  35. };