【发布时间】:2014-06-17 21:23:53
【问题描述】:
以下是域类:
class AuthorAR {
private $authorId;
}
class BookAR {
private $bookId;
// book owner
private $authorId;
private $title;
public function changeTitle($title) {
$this->title = $title;
}
}
// This will be in the domain layer to make explicit the finding of the book
interface BookRepository {
public function findByBookId($bookId);
public function findForAuthorIdByBookId($authorId, $bookId);
}
这是用于域外授权的 Dao 类:
class AuthorizationDao {
public function findBookOfIdForAuthorId($bookId, $authorId) {}
}
这是我在某些地方看到的 2 个方法,不知道哪个更好,哪个被认为是好的做法(这只是一个幼稚的例子,主要问题是放置此类授权的位置):
// Aproach 1 : call the repository with the method made explicit in the domain,
// in order to check if the book with a specific author exists
class ChangeBookTitleCommandHander {
public function handle($command) {
$book = $bookRepository->findForAuthorIdByBookId($command->authorId, $command->bookId );
if($book === NULL) {
throw new CommandHandlingFailedException();
}
}
}
// Aproach 2 use an authorization service inside the controller to check if a user
// has access to the specific book resource in order to change it's title
class Controller {
public function changeTitleAction() {
// This will use the authorizationDao->findBookOfIdForAuthorId($bookId, $authorId) to allow
// access for changing that resource
// @throws UnauthorizeAccessException
$authorizationService->authorizeCommand($changeBookTitleCommand);
}
}
那么如何设计这种权限检查(授权)呢?在允许 BookAR 更改其状态之前,如果特定作者是该书的所有者,如何设计验证(对于不需要查询权限的 CQRS,只需状态更改权限)?
【问题讨论】:
标签: design-patterns authorization domain-driven-design cqrs