40 lines
1.2 KiB
C#
40 lines
1.2 KiB
C#
using OFBButte.Application.Configuration;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace OFBButte.Application.Base
|
|
{
|
|
public class AuthorizeCommand
|
|
{
|
|
private readonly IAccess access;
|
|
protected readonly IEnumerable<Permission> allowedPermissions;
|
|
private AuthorizeCommand(IAccess access)
|
|
{
|
|
this.access = access;
|
|
}
|
|
public AuthorizeCommand(IAccess access, Permission permission): this(access)
|
|
{
|
|
this.allowedPermissions = new Permission[] { permission };
|
|
}
|
|
public AuthorizeCommand(IAccess access, IEnumerable<Permission> permissions): this(access)
|
|
{
|
|
this.allowedPermissions = permissions;
|
|
}
|
|
|
|
public void Authorize()
|
|
{
|
|
if (allowedPermissions == null)
|
|
{
|
|
throw new UnauthorizedAccessException("Permission Denied");
|
|
}
|
|
foreach(var permission in allowedPermissions)
|
|
{
|
|
if (access.CanDo(permission))
|
|
return;
|
|
}
|
|
throw new UnauthorizedAccessException($"Permission Denied: {string.Join(", ", allowedPermissions)}");
|
|
}
|
|
}
|
|
}
|