Hiding the Tenant Administration Permission

We now have one little problem. User tenant2 has permission Administration:Security so he can access user and role permission dialogs. Thus, he can grant himself Administration:Tenants permission using the permission UI.

Tenant2 Granting Himself

Serenity scans your assemblies for attributes like ReadPermission, WritePermission, PageAuthorize, ServiceAuthorize etc. and lists these permissions in edit permissions dialog.

We should first remove it from this pre-populated list.

Find method, ListPermissionKeys in UserPermissionRepository.cs:

  1. public ListResponse<string> ListPermissionKeys()
  2. {
  3. return LocalCache.Get("Administration:PermissionKeys", TimeSpan.Zero, () =>
  4. {
  5. //...
  6. result.Remove(Administration.PermissionKeys.Tenants);
  7. result.Remove("*");
  8. result.Remove("?");
  9. //...

Now, this permission won’t be listed in Edit User Permissions or Edit Role Permissions dialog.

But, still, he can grant this permission to himself, by some little hacking through UserPermissionRepository.Update or RolePermissionRepository.Update methods.

We should add some checks to prevent this:

  1. public class UserPermissionRepository
  2. {
  3. public SaveResponse Update(IUnitOfWork uow,
  4. UserPermissionUpdateRequest request)
  5. {
  6. //...
  7. var newList = new Dictionary<string, bool>(
  8. StringComparer.OrdinalIgnoreCase);
  9. foreach (var p in request.Permissions)
  10. newList[p.PermissionKey] = p.Grant ?? false;
  11. var allowedKeys = ListPermissionKeys()
  12. .Entities.ToDictionary(x => x);
  13. if (newList.Keys.Any(x => !allowedKeys.ContainsKey(x)))
  14. throw new AccessViolationException();
  15. //...
  1. public class RolePermissionRepository
  2. {
  3. public SaveResponse Update(IUnitOfWork uow,
  4. RolePermissionUpdateRequest request)
  5. {
  6. //...
  7. var newList = new HashSet<string>(
  8. request.Permissions.ToList(),
  9. StringComparer.OrdinalIgnoreCase);
  10. var allowedKeys = new UserPermissionRepository()
  11. .ListPermissionKeys()
  12. .Entities.ToDictionary(x => x);
  13. if (newList.Any(x => !allowedKeys.ContainsKey(x)))
  14. throw new AccessViolationException();
  15. //...

Here we check if any of the new permission keys that are tried to be granted, are not listed in permission dialog. If so, this is probably a hack attempt.

Actually this check should be the default, even without multi-tenant systems, but usually we trust administrative users. Here, administrators will be only managing their own tenants, so we certainly need this check.