RevokePermissionAction.vala 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using Statum;
  2. namespace Spry.Actions {
  3. /**
  4. * Revokes a single permission from the user targeted by the sealed
  5. * private data ({@link UserActionPrivate}.`user_id`, falling back to
  6. * the `user_id` form field when no blob was sent); the permission
  7. * comes from {@link UserManagementAction.target_permission}, and only
  8. * the matching rows are removed (the user's remaining permissions are
  9. * untouched). A missing target user is refused with the failure
  10. * notification rather than reporting an empty revoke as success.
  11. * Admin-guarded.
  12. */
  13. public class RevokePermissionAction : UserManagementAction {
  14. /** Notification shown when no `user_id` was supplied. */
  15. protected virtual string user_required_message { get { return "A user id is required."; } }
  16. /** Notification shown when no permission was supplied. */
  17. protected virtual string permission_required_message { get { return "A permission is required."; } }
  18. /** Notification shown after a successful revoke. */
  19. protected virtual string permission_revoked_message { get { return "Permission revoked."; } }
  20. /** Notification shown when the revoke could not be persisted. */
  21. protected virtual string revoke_failed_message { get { return "Could not revoke the permission. Please try again later."; } }
  22. public override async DirectiveBuilder handle() throws GLib.Error {
  23. var guard = yield require_permission_guard();
  24. if (guard != null) {
  25. return (!)guard;
  26. }
  27. int64 user_id = target_user_id();
  28. if (user_id == 0) {
  29. return directives().notify("error", user_required_message);
  30. }
  31. string permission = target_permission();
  32. if (permission.length == 0) {
  33. return directives().notify("error", permission_required_message);
  34. }
  35. try {
  36. var target = yield users.get_user(user_id);
  37. if (target == null) {
  38. warning("[Spry] revoke refused: no user exists with id %d", (int) user_id);
  39. return directives().notify("error", revoke_failed_message);
  40. }
  41. yield users.remove_user_permission(user_id, permission);
  42. return directives().notify("success", permission_revoked_message);
  43. } catch (Error e) {
  44. return directives().notify("error",
  45. ProfileValidation.internal_error_message(e, revoke_failed_message));
  46. }
  47. }
  48. }
  49. }