GrantPermissionAction.vala 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using Statum;
  2. namespace Spry.Actions {
  3. /**
  4. * Grants a permission to the user targeted by the sealed private data
  5. * ({@link UserActionPrivate}.`user_id`, falling back to the `user_id`
  6. * form field when no blob was sent); the permission comes from {@link
  7. * UserManagementAction.target_permission}. Idempotent — granting a
  8. * permission the user already holds succeeds without duplicating the
  9. * row. A missing target user is refused with the failure notification,
  10. * so a stale id cannot orphan a permission row. Admin-guarded.
  11. */
  12. public class GrantPermissionAction : UserManagementAction {
  13. /** Notification shown when no `user_id` was supplied. */
  14. protected virtual string user_required_message { get { return "A user id is required."; } }
  15. /** Notification shown when no permission was supplied. */
  16. protected virtual string permission_required_message { get { return "A permission is required."; } }
  17. /** Notification shown after a successful grant. */
  18. protected virtual string permission_granted_message { get { return "Permission granted."; } }
  19. /** Notification shown when the grant could not be persisted. */
  20. protected virtual string grant_failed_message { get { return "Could not grant the permission. Please try again later."; } }
  21. public override async DirectiveBuilder handle() throws GLib.Error {
  22. var guard = yield require_permission_guard();
  23. if (guard != null) {
  24. return (!)guard;
  25. }
  26. int64 user_id = target_user_id();
  27. if (user_id == 0) {
  28. return directives().notify("error", user_required_message);
  29. }
  30. string permission = target_permission();
  31. if (permission.length == 0) {
  32. return directives().notify("error", permission_required_message);
  33. }
  34. try {
  35. var target = yield users.get_user(user_id);
  36. if (target == null) {
  37. warning("[Spry] grant refused: no user exists with id %d", (int) user_id);
  38. return directives().notify("error", grant_failed_message);
  39. }
  40. yield users.set_user_permission(user_id, permission);
  41. return directives().notify("success", permission_granted_message);
  42. } catch (Error e) {
  43. return directives().notify("error",
  44. ProfileValidation.internal_error_message(e, grant_failed_message));
  45. }
  46. }
  47. }
  48. }