Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere to verify authorization for a Command?
    primarykey
    data
    text
    <p>The question's title resumes pretty much: where do I verify authorization for a Command?</p> <p>For example, setting a customer as preferred involves:</p> <ul> <li><code>MarkAsPreferred</code> controller action (could be Winforms or whatever);</li> <li><code>SetCustomerAsPreferredCommand</code>;</li> <li><code>SetCustomerAsPreferredCommandHandler</code>;</li> <li><code>Customer.MarkAsPreferred()</code> (domain);</li> </ul> <p>I identified 3 places to check for authorization:</p> <ul> <li><strong>UI</strong> for displaying purposes (user should not see a link/button if he/she does not have access to it);</li> <li><strong>controller action</strong> to verify the user is authorized to call that command; commands are assumed to always succeed (regarding validation, but I'm assuming authorization too) and we have a chance to inform the user about lack of access;</li> <li><strong>inside the command</strong> just before calling domain logic;</li> </ul> <p><strong>SomeView.cshtml</strong></p> <pre><code>if (authorizationService.Authorize("MarkCustomerAsPreferred)) { // show link } </code></pre> <p><strong>CustomerController</strong></p> <pre><code>[HttpPost] public ActionResult MarkAsPreferred(Guid id) { if (!authorizationService.Authorize("MarkCustomerAsPreferred)) { return RedirectToAction("Unauthorized"); } var MarkCustomerAsPreferredCommand { Id = id }; ... } </code></pre> <p><strong>MarkCustomerAsPreferredCommandHandler</strong></p> <pre><code>public void Handle(MarkCustomerAsPreferredCommand command) { if (!authorizationService.Authorize("MarkCustomerAsPreferred")) { throw new Exception("..."); } customer.MarkAsPreferred(); } </code></pre> <p>My question is: Do I <strong>need</strong> to verify authorization in 3 places or I'm just being overzealous?</p> <p>I searched all over the internet but couldn't find any example or reference about this.</p> <p><strong>Edit</strong></p> <p>After more research and some tests I think wrapping the commands to add behavior (authorization, validation, logging) as Dennis Taub suggested is easier and cleaner to implement.</p> <p>I found <a href="https://cuttingedge.it/blogs/steven/pivot/entry.php?id=91">this blog post</a> which explains exactly this concept.</p> <p>About having multiple handlers for one command, I don't need to implement one command handler for each behavior for each original command, one wrapping command can wrap all handlers.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload