# 권한을 강제하는 가장 좋은 방법은 무엇인가요? 컨트롤러인지 제약 조건인지?

**URL:** <https://meta.discourse.org/t/best-way-to-enforce-permissions-controller-or-constraint/176113>\
**Category:** Development\
**Created:** [1월 14, 2021, 5:02오후 UTC](https://meta.discourse.org/t/best-way-to-enforce-permissions-controller-or-constraint/176113 "2021-01-14T17:02:56Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [1월 14, 2021, 5:02오후 UTC](https://meta.discourse.org/t/best-way-to-enforce-permissions-controller-or-constraint/176113/1 "2021-01-14T17:02:56Z")

</div>

`server` 모델을 추가하는 플러그인을 하나 작성했습니다. 이 모델에는 `user_id` 필드가 있습니다. 저는 `server`의 소유자만 `POST` 요청을(아니면 `PUT`이어야 할까요? — 이 동작은 사용자의 서버에서 Discourse 인스턴스를 커맨드 라인으로 재빌드합니다. 어떤 것이 _베스트 프랙티스_인지 확실하지 않습니다.) 수행할 수 있도록 하고 싶습니다.

`config/routes.rb`에는 다음과 같은 코드가 있습니다:

post “/upgrade/:id” =\> “servers#queue\_upgrade”, constraints: PfaffmanagerConstraint.new

`pfaffmanager_constraint.rb`의 `matches?`에서 `path_parameters[:action]`를 확인하고在那里 권한을 강제하는 것이 권장되나요, 아니면 `servers_controller.rb`에서 수행하는 것이 좋나요?

또한, 이 라우트를 `namespace :user` 아래로 이동하는 것이 실제로 해야 할 일이라고 생각합니다. 그래서 그 부분을 파악하면 이 문제도 자연스럽게 해결될 수 있을 것 같지만, 현재는 거의 작동하고 있으며 다른 사람들이 이 코드를 시도해 볼 수 있도록 하고 싶습니다.

Rails 라우트는 다음과 같습니다:  
[https://github.com/pfaffman/discourse-pfaffmanager/blob/master/config/routes.rb#L12](https://github.com/pfaffman/discourse-pfaffmanager/blob/master/config/routes.rb#L12)

그리고 제 컨트롤러는 권한을 강제하려고 시도하고 있습니다:  
[https://github.com/pfaffman/discourse-pfaffmanager/blob/master/app/controllers/pfaffmanager/servers\_controller.rb#L53-L70](https://github.com/pfaffman/discourse-pfaffmanager/blob/master/app/controllers/pfaffmanager/servers_controller.rb#L53-L70)

하지만 다른 사용자가 `post` 요청을 수행할 때 제 스펙은 200을 반환하고 있습니다:  
[https://github.com/pfaffman/discourse-pfaffmanager/blob/master/spec/requests/servers\_controller\_spec.rb#L149-L157](https://github.com/pfaffman/discourse-pfaffmanager/blob/master/spec/requests/servers_controller_spec.rb#L149-L157)

아마도 Ember가 이 부분을 인식하도록 somehow 조치를 취해야 할 것 같습니다.

---

_[View the full topic](https://meta.discourse.org/t/best-way-to-enforce-permissions-controller-or-constraint/176113)._
