Base solution for your next web application
Open Closed

GetPermission in Backgroundjob #9532


User avatar
0
Web2workNL created

Hai,

My project has a situation where I want to get a Permission from the PermissionManager inside a backgroundjob. The permission is defined as follows:

administration.CreateChildPermission(AppPermissions.Pages_Administration_TeacherGroups, L("GroupsForTeachers"), multiTenancySides: MultiTenancySides.Tenant);

inside the AppAuthorizationProvider. When the following code is run

using (CurrentUnitOfWork.SetTenantId(tenantId))
                {                    
                    using (_session.Use(tenantId, null))
                    {
                      
                        var permission = _permissionManager.GetPermissionOrNull(AppPermissions.Pages_Administration_TeacherGroups);
                     }
                }
                

the permission is always NULL. What am I missing?

We are currently using ABP 4.12.0


3 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    hi

    Is the _permissionManager service created through dependency injection?

    Please share some code of you backgroundjob.

  • User Avatar
    0
    Web2workNL created

    Yes, the PermissionManager is created through dependency injection. So is the _session btw private readonly IAbpSession _session;

    The background job is not a timed job but is handled like the notifications are (through AbpBackgoundJobs)

      await _backgroundJobManager.EnqueueAsync<SyncWithMooJob, SyncWithMooJobArgs>(new SyncWithMooJobArgs(AbpSession.GetTenantId()));
    

    And

     public class SyncWithMooJob : BackgroundJob<SyncWithMooJobArgs>, ITransientDependency
            {
                private readonly IMooManager _mooSyncManager;
                
                public SyncWithMooJob(IMooManager mooSyncManager)
                {
                    _mooSyncManager = mooSyncManager;
                }
    
                public override void Execute(SyncWithMooJobArgs args)
                {
                    AsyncHelper.RunSync(() => _mooSyncManager.AutomaticSyncWithMoo(args.TenantId));
                }
            }
        }
    

    If I strip the method down to just

    
            public async Task AutomaticSyncWithMoo(int tenantId)
            {
                try
                {
                    using (CurrentUnitOfWork.SetTenantId(tenantId))
                    {
                       
                        using (_session.Use(tenantId, null))
                        { 
                        var permission = _permissionManager.GetPermissionOrNull(AppPermissions.Pages_Administration_TeacherGroups);
                         }
                    } 
                }
                catch (Exception ex)
                {
                    Logger.Error(ex.Message + ex.StackTrace);
                }
            }
                        
    

    I still get a NULL result. Just for clarity, when I add _permissionManager.GetPermissionOrNull(AppPermissions.Pages_Administration_TeacherGroups); to for instance an AppService there are no issues

  • User Avatar
    0
    maliming created
    Support Team

    hi @web2worknl

    Can you reprodece the problem via the free startup template and share it?

    https://aspnetboilerplate.com/Templates