Web Application Development Tutorial - Part 5: Authorization

About This Tutorial

In this tutorial series, you will build an ABP based web application named Acme.BookStore. This application is used to manage a list of books and their authors. It is developed using the following technologies:

  • Entity Framework Core as the ORM provider.
  • MVC / Razor Pages as the UI Framework.

This tutorial is organized as the following parts;

Download the Source Code

This tutorial has multiple versions based on your UI and Database preferences. We’ve prepared two combinations of the source code to be downloaded:

Video Tutorial

This part is also recorded as a video tutorial and published on YouTube.

Permissions

ABP Framework provides an authorization system based on the ASP.NET Core’s authorization infrastructure. One major feature added on top of the standard authorization infrastructure is the permission system which allows to define permissions and enable/disable per role, user or client.

Permission Names

A permission must have a unique name (a string). The best way is to define it as a const, so we can reuse the permission name.

Open the BookStorePermissions class inside the Acme.BookStore.Application.Contracts project (in the Permissions folder) and change the content as shown below:

  1. namespace Acme.BookStore.Permissions
  2. {
  3. public static class BookStorePermissions
  4. {
  5. public const string GroupName = "BookStore";
  6. public static class Books
  7. {
  8. public const string Default = GroupName + ".Books";
  9. public const string Create = Default + ".Create";
  10. public const string Edit = Default + ".Edit";
  11. public const string Delete = Default + ".Delete";
  12. }
  13. }
  14. }

This is a hierarchical way of defining permission names. For example, “create book” permission name was defined as BookStore.Books.Create.

Permission Definitions

You should define permissions before using them.

Open the BookStorePermissionDefinitionProvider class inside the Acme.BookStore.Application.Contracts project (in the Permissions folder) and change the content as shown below:

  1. using Acme.BookStore.Localization;
  2. using Volo.Abp.Authorization.Permissions;
  3. using Volo.Abp.Localization;
  4. namespace Acme.BookStore.Permissions
  5. {
  6. public class BookStorePermissionDefinitionProvider : PermissionDefinitionProvider
  7. {
  8. public override void Define(IPermissionDefinitionContext context)
  9. {
  10. var bookStoreGroup = context.AddGroup(BookStorePermissions.GroupName, L("Permission:BookStore"));
  11. var booksPermission = bookStoreGroup.AddPermission(BookStorePermissions.Books.Default, L("Permission:Books"));
  12. booksPermission.AddChild(BookStorePermissions.Books.Create, L("Permission:Books.Create"));
  13. booksPermission.AddChild(BookStorePermissions.Books.Edit, L("Permission:Books.Edit"));
  14. booksPermission.AddChild(BookStorePermissions.Books.Delete, L("Permission:Books.Delete"));
  15. }
  16. private static LocalizableString L(string name)
  17. {
  18. return LocalizableString.Create<BookStoreResource>(name);
  19. }
  20. }
  21. }

This class defines a permission group (to group permissions on the UI, will be seen below) and 4 permissions inside this group. Also, Create, Edit and Delete are children of the BookStorePermissions.Books.Default permission. A child permission can be selected only if the parent was selected.

Finally, edit the localization file (en.json under the Localization/BookStore folder of the Acme.BookStore.Domain.Shared project) to define the localization keys used above:

  1. "Permission:BookStore": "Book Store",
  2. "Permission:Books": "Book Management",
  3. "Permission:Books.Create": "Creating new books",
  4. "Permission:Books.Edit": "Editing the books",
  5. "Permission:Books.Delete": "Deleting the books"

Localization key names are arbitrary and no forcing rule. But we prefer the convention used above.

Permission Management UI

Once you define the permissions, you can see them on the permission management modal.

Go to the Administration -> Identity -> Roles page, select Permissions action for the admin role to open the permission management modal:

bookstore-permissions-ui

Grant the permissions you want and save the modal.

Authorization

Now, you can use the permissions to authorize the book management.

Application Layer & HTTP API

Open the BookAppService class and add set the policy names as the permission names defined above:

  1. using System;
  2. using Acme.BookStore.Permissions;
  3. using Volo.Abp.Application.Dtos;
  4. using Volo.Abp.Application.Services;
  5. using Volo.Abp.Domain.Repositories;
  6. namespace Acme.BookStore.Books
  7. {
  8. public class BookAppService :
  9. CrudAppService<
  10. Book, //The Book entity
  11. BookDto, //Used to show books
  12. Guid, //Primary key of the book entity
  13. PagedAndSortedResultRequestDto, //Used for paging/sorting
  14. CreateUpdateBookDto>, //Used to create/update a book
  15. IBookAppService //implement the IBookAppService
  16. {
  17. public BookAppService(IRepository<Book, Guid> repository)
  18. : base(repository)
  19. {
  20. GetPolicyName = BookStorePermissions.Books.Default;
  21. GetListPolicyName = BookStorePermissions.Books.Default;
  22. CreatePolicyName = BookStorePermissions.Books.Create;
  23. UpdatePolicyName = BookStorePermissions.Books.Edit;
  24. DeletePolicyName = BookStorePermissions.Books.Delete;
  25. }
  26. }
  27. }

Added code to the constructor. Base CrudAppService automatically uses these permissions on the CRUD operations. This makes the application service secure, but also makes the HTTP API secure since this service is automatically used as an HTTP API as explained before (see auto API controllers).

Razor Page

While securing the HTTP API & the application service prevents unauthorized users to use the services, they can still navigate to the book management page. While they will get authorization exception when the page makes the first AJAX call to the server, we should also authorize the page for a better user experience and security.

Open the BookStoreWebModule and add the following code block inside the ConfigureServices method:

  1. Configure<RazorPagesOptions>(options =>
  2. {
  3. options.Conventions.AuthorizePage("/Books/Index", BookStorePermissions.Books.Default);
  4. options.Conventions.AuthorizePage("/Books/CreateModal", BookStorePermissions.Books.Create);
  5. options.Conventions.AuthorizePage("/Books/EditModal", BookStorePermissions.Books.Edit);
  6. });

Now, unauthorized users are redirected to the login page.

Hide the New Book Button

The book management page has a New Book button that should be invisible if the current user has no Book Creation permission.

bookstore-new-book-button-small

Open the Pages/Books/Index.cshtml file and change the content as shown below:

  1. @page
  2. @using Acme.BookStore.Localization
  3. @using Acme.BookStore.Permissions
  4. @using Acme.BookStore.Web.Pages.Books
  5. @using Microsoft.AspNetCore.Authorization
  6. @using Microsoft.Extensions.Localization
  7. @model IndexModel
  8. @inject IStringLocalizer<BookStoreResource> L
  9. @inject IAuthorizationService AuthorizationService
  10. @section scripts
  11. {
  12. <abp-script src="/Pages/Books/Index.js"/>
  13. }
  14. <abp-card>
  15. <abp-card-header>
  16. <abp-row>
  17. <abp-column size-md="_6">
  18. <abp-card-title>@L["Books"]</abp-card-title>
  19. </abp-column>
  20. <abp-column size-md="_6" class="text-right">
  21. @if (await AuthorizationService.IsGrantedAsync(BookStorePermissions.Books.Create))
  22. {
  23. <abp-button id="NewBookButton"
  24. text="@L["NewBook"].Value"
  25. icon="plus"
  26. button-type="Primary"/>
  27. }
  28. </abp-column>
  29. </abp-row>
  30. </abp-card-header>
  31. <abp-card-body>
  32. <abp-table striped-rows="true" id="BooksTable"></abp-table>
  33. </abp-card-body>
  34. </abp-card>
  • Added @inject IAuthorizationService AuthorizationService to access to the authorization service.
  • Used @if (await AuthorizationService.IsGrantedAsync(BookStorePermissions.Books.Create)) to check the book creation permission to conditionally render the New Book button.

JavaScript Side

Books table in the book management page has an actions button for each row. The actions button includes Edit and Delete actions:

bookstore-edit-delete-actions

We should hide an action if the current user has not granted for the related permission. Datatables row actions has a visible option that can be set to false to hide the action item.

Open the Pages/Books/Index.js inside the Acme.BookStore.Web project and add a visible option to the Edit action as shown below:

  1. {
  2. text: l('Edit'),
  3. visible: abp.auth.isGranted('BookStore.Books.Edit'), //CHECK for the PERMISSION
  4. action: function (data) {
  5. editModal.open({ id: data.record.id });
  6. }
  7. }

Do same for the Delete action:

  1. visible: abp.auth.isGranted('BookStore.Books.Delete')
  • abp.auth.isGranted(...) is used to check a permission that is defined before.
  • visible could also be get a function that returns a bool if the value will be calculated later, based on some conditions.

Menu Item

Even we have secured all the layers of the book management page, it is still visible on the main menu of the application. We should hide the menu item if the current user has no permission.

Open the BookStoreMenuContributor class, find the code block below:

  1. context.Menu.AddItem(
  2. new ApplicationMenuItem(
  3. "BooksStore",
  4. l["Menu:BookStore"],
  5. icon: "fa fa-book"
  6. ).AddItem(
  7. new ApplicationMenuItem(
  8. "BooksStore.Books",
  9. l["Menu:Books"],
  10. url: "/Books"
  11. )
  12. )
  13. );

And replace this code block with the following:

  1. var bookStoreMenu = new ApplicationMenuItem(
  2. "BooksStore",
  3. l["Menu:BookStore"],
  4. icon: "fa fa-book"
  5. );
  6. context.Menu.AddItem(bookStoreMenu);
  7. //CHECK the PERMISSION
  8. if (await context.IsGrantedAsync(BookStorePermissions.Books.Default))
  9. {
  10. bookStoreMenu.AddItem(new ApplicationMenuItem(
  11. "BooksStore.Books",
  12. l["Menu:Books"],
  13. url: "/Books"
  14. ));
  15. }

The Next Part

See the next part of this tutorial.