B3.02 Security: Authorization (AdminUsers and AdminRoles Features) (Xamarin) - chempkovsky/CS2WPF-and-CS2XAMARIN GitHub Wiki

Instead of adding all security components into Navbar we define a pair of Features with security components as a child.

AdminRoles Feature

  • Create asp\AdminRoles\Views-subfolder under FeatureServicesPrismModule-project
  • Create asp\AdminRoles\ViewModels-subfolder under FeatureServicesPrismModule-project
  • Right click asp\AdminRoles\Views-folder
  • Open FeatureScripts Wizard

DB structure

  • On the first page click "Next"-button

DB structure

  • On the second page
    • select aspnetchckdbcontext-DBcontext
    • click Next-button

DB structure

  • On the third page
    • click Add-button
    • Feature-dialog will be shown

DB structure

  • In Feature-dialog
    • Type AdminUsers
    • check
      • aspnetdashboardView (01598-LformUserControl.xaml)
      • aspnetmodelView (01598-LformUserControl.xaml)
      • aspnetroleView (01598-LformUserControl.xaml)
      • aspnetroleView (02122-O2mMaskUserControl.xaml)
      • aspnetrolemaskView (01598-LformUserControl.xaml)
    • click Save button

DB structure

  • You are On the third page again
    • click Next-button

DB structure

  • On the fourth page
    • Select "30100-FtrUserControl.xaml.cs"
    • Click Next-button

DB structure

  • On the fifth page
    • Select "FtrUserControl.Xamarin.xaml.cs.t4"
    • Click "Next"-button

DB structure

  • On the sixth page
    • Click "Save"-button
    • Click "Next"-button

DB structure

  • You are On the third page again
    • click Next-button

DB structure

  • On the fourth page
    • Select "30102-FtrUserControl.xaml"
    • Click Next-button

DB structure

  • On the fifth page
    • Select "FtrUserControl.Xamarin.xaml.t4"
    • Click "Next"-button

DB structure

  • On the sixth page
    • Click "Save"-button
    • close the dialog

DB structure

  • Right click asp\AdminRoles\ViewModels-folder
  • Open FeatureScripts Wizard

DB structure

  • On the first page click "Next"-button

DB structure

  • On the third page
    • select "AdminRoles"
    • click Next-button

DB structure

  • On the fourth page
    • Select "30104-FtrViewModel.cs"
    • Click Next-button

DB structure

  • On the fifth page
    • Select "FtrViewModel.Xamarin.cs.t4"
    • Click "Next"-button

DB structure

  • On the sixth page

    • Click "Save"-button
    • Click "Next"-button
  • You are On the third page again

    • click Next-button

DB structure

  • On the fourth page
    • Select "30104-FtrViewModel.PrismModuleModifier.txt"
    • Click Next-button

DB structure

  • On the fifth page
    • Select "30104-FtrViewModel.PrismModuleModifier.txt.t4"
    • Click "Next"-button

DB structure

  • On the sixth page
    • Click "Save"-button
    • close the dialog

DB structure

open "AdminRolesFtrViewModel.cs"-file and DO NOT follow the instructions

DB structure

  • Under FeatureServicesPrismModule-project the RegisterTypes()-method of the FeatureServicesPrismModuleModule.cs becomes as follows
        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            ... 
            Prism.Mvvm.ViewModelLocationProvider.Register<FeatureServicesPrismModule.asp.AdminRoles.Views.AdminRolesFtrUserControl, FeatureServicesPrismModule.asp.AdminRoles.ViewModels.AdminRolesFtrViewModel>();
            containerRegistry.RegisterForNavigation<FeatureServicesPrismModule.asp.AdminRoles.Views.AdminRolesFtrUserControl, FeatureServicesPrismModule.asp.AdminRoles.ViewModels.AdminRolesFtrViewModel>("AdminRolesFtrUserControl");
         }

We need only the name "AdminRolesFtrUserControl"

  • Open "Views\MainFlyoutPage.xaml"-file of the PrismDemoApp-project
  • Modify content of StackLayout as shown:
...
                    <Label Text="Admin"/>
                    <Button BackgroundColor="Transparent"  Text="Admin Roles"  Command="{Binding NavigateCommand}" CommandParameter="AdminRolesFtrUserControl" />

  • run Dm04WebApp-app
  • run PrismDemoApp-app
  • click "Admin Roles"-navigation menu
  • here is a result

DB structure

AdminUsers Feature

  • Create asp\AdminUsers\Views-subfolder under FeatureServicesPrismModule-project
  • Create asp\AdminUsers\ViewModels-subfolder under FeatureServicesPrismModule-project
  • Repeat the steps described above to create AdminUsers Feature
  • Include in the list of the AdminUsers Feature:
    • aspnetroleView (01598-LformUserControl.xaml)
    • aspnetuserView (01598-LformUserControl.xaml)
    • aspnetuserView (02122-O2mMaskUserControl.xaml)
    • aspnetuserView (01698-O2mUserControl.xaml)
    • aspnetuserrolesView (01598-LformUserControl.xaml)

DB structure

open "AdminUsersFtrViewModel.cs"-file and DO NOT follow the instructions

DB structure

  • Under FeatureServicesPrismModule-project the RegisterTypes()-method of the FeatureServicesPrismModuleModule.cs becomes as follows
        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            ... 
            Prism.Mvvm.ViewModelLocationProvider.Register<FeatureServicesPrismModule.asp.AdminUsers.Views.AdminUsersFtrUserControl, FeatureServicesPrismModule.asp.AdminUsers.ViewModels.AdminUsersFtrViewModel>();
            containerRegistry.RegisterForNavigation<FeatureServicesPrismModule.asp.AdminUsers.Views.AdminUsersFtrUserControl, FeatureServicesPrismModule.asp.AdminUsers.ViewModels.AdminUsersFtrViewModel>("AdminUsersFtrUserControl");
         }

We need only the name "AdminUsersFtrUserControl"

  • Open "Views\MainFlyoutPage.xaml"-file of the PrismDemoApp-project
  • Modify content of StackLayout as shown:
...
                    <Label Text="Admin"/>
                    <Button BackgroundColor="Transparent"  Text="Admin Roles"  Command="{Binding NavigateCommand}" CommandParameter="AdminRolesFtrUserControl" />
                    <Button BackgroundColor="Transparent"  Text="Admin Users"  Command="{Binding NavigateCommand}" CommandParameter="AdminUsersFtrUserControl" />

  • run Dm04WebApp-app
  • run PrismDemoApp-app
  • click "Admin Users"-navigation menu
  • here is a result

DB structure

How to use Admin Features

With "aspnetroleView (01598-LformUserControl.xaml)" add roles to the App.

DB structure

With "aspnetuserView (01698-O2mUserControl.xaml)" assign roles to the user.

DB structure

  • in db

DB structure

With "aspnetrolemaskView (01598-LformUserControl.xaml)" add permission mask to each role.

DB structure

  • in db

DB structure

With "aspnetroleView (02122-O2mMaskUserControl.xaml)" modify permission mask for each role.

DB structure

With "aspnetuserView (02122-O2mMaskUserControl.xaml)" you can view permission mask for the given user.

  • Note: you can not change permission mask with current component. This component is useful if more than one role has been assigned to a given user.

DB structure

With "aspnetdashboardView (01598-LformUserControl.xaml)" you can assign code to feature

DB structure

  • in db

DB structure

With "aspnetmodelView (01598-LformUserControl.xaml)" you can assign code to view

DB structure

  • in db

DB structure

aspnetuserrolesView (01598-LformUserControl.xaml) never returns rows

Please take a look at the code of "aspnetuserrolesViewWebApiController.cs"-file

        [HttpGet]
        [Route("getwithfilter")]
        [ResponseType(typeof(aspnetuserrolesViewPage))]
        public IHttpActionResult getwithfilter([FromUri] System.String[] userId 
                  , [FromUri] string[] userIdOprtr 
                , [FromUri] System.String[] roleId 
                  , [FromUri] string[] roleIdOprtr 
                , [FromUri] System.String[] uUserName 
                  , [FromUri] string[] uUserNameOprtr 
                , [FromUri] System.String[] rName 
                  , [FromUri] string[] rNameOprtr 
                , [FromUri] string[] orderby = null, [FromUri] int? page =null, [FromUri] int? pagesize = null)
        {

            string[] EqualOperators = { "eq", "lk" };
            string[] ExpectedOperators = { "eq", "lk", "gt", "lt", "ne" };

            int currentPageSize = this.defaultPageSize;
            int currentPage = 1;
            if (pagesize.HasValue) {
                currentPageSize = pagesize.Value;
                if ((currentPageSize < this.minPageSize) || (currentPageSize > this.maxPageSize)) {
                    currentPageSize = defaultPageSize;
                }
            }
            if (page.HasValue) {
                currentPage = page.Value+1;
                if (currentPage < 1) {
                    currentPage = 1;
                }
            }

            bool hasNo = true;
            System.String UserId = null;
            if(userId != null) {
                if(userId.Length > 0) {
                    int filterCnt = userId.Length;
                    for(int i = 0; i < filterCnt; i++) {
                        if(  string.IsNullOrEmpty(userId[i]) ) continue;
                        UserId = userId[i];
                        hasNo = false;
                        break;
                    }

                }
            }
            aspnetuserrolesViewPage resultObject = new aspnetuserrolesViewPage() {
                page = (currentPage > 0) ? (currentPage-1) : currentPage,
                pagesize = currentPageSize,
                pagecount = 0,
                total = 0,
                items = new List<aspnetuserrolesView>()
            };

            if (hasNo) {
                return Ok(resultObject);
            }
            ApplicationUser usr = UserManager.Users.Where(u => u.Id == UserId).FirstOrDefault();
            if(usr == null) {
                return Ok(resultObject);
            }
            ...

"aspnetuserrolesView (01598-LformUserControl.xaml)"-component can be used as detail part of O2m-control. So remove it from the "AdminUsersFtrUserControl".