我有代表用户授权的类(scala代码):
case class Authorization(
userId: UUID,
permissions: Seq[String],
userRoles: Seq[String],
companyId: UUID
) {
private val policy = new Policy(permissions)
def isAllowed(path: String, action: String): Boolean = policy.isAllowed(path, action)
def isSuperuser: Boolean = userRoles.contains(SuperUser)
}
该类在应用程序中的许多地方使用,以检查用户是否对特定实体具有特定权限,例如:
authorization.isAllowed(s"some/entity/path/$id", "read")
为了提高可用性并避免直接的字符串操作,我将这些方法包装在更具体的方法中:
def canViewCompany(companyId:UUID)=authorization.isAllowed(s"company/$companyId","read")
def canViewUser(userId:UUID)=authorization.isAllowed(s"user/$userId","read")
def canEditTask(id:UUID)=authorization.isAllowed(....)
....
以此类推,最多 20 种方法。应该Authorization
包含这样的方法canViewCompany()
吗?
此类是否有责任了解每个特定实体检查?
更新:所以最终的问题是:
canViewCompany(companyId:UUID)
为了提高类的可用性而创建包装器是个好主意 吗?- 如果是这样,应该将这些方法
Authorization
本身替换还是提取到某些服务中,例如:PermissionService.canViewCompany(id,authorization)=authorization.isAllowed()?