나는이 함께 볼 수있는 문제는
그런 다음 당신은 그것을 처리하는 일반적인 서비스를 필요 이 응용 폭이 될 것입니다. 어쨌든 IAuthorisationService라고 부릅니다.
나는 이 접근하는 방법에 대한 모범 사례를 찾고 있습니다. CustomControllers에서 을 볼 수 있습니까? 필터? 또는 무엇?
위의 공통 IAuthorisationService를 사용해야합니다.
:
/* Interfaces */
public interface IAuthorisationService {
bool CanEdit(YourItem item);
}
public interface ICurrentUserProvider {
YourUserEntity GetCurrentUser();
}
/* Implementations */
public class HttpUserProvider : ICurrentUserProvider {
public YourUserEntity GetCurrentUser() {
return HttpContext.Current.User.Principal as YourUserEntity;
}
}
public calss RolesAuthorisationService : IAuthorisationService {
ICurrentUserProvider userProvider
public RolesAuthorisationService(ICurrentUserProvider userProvider) {
this.userProvider = userProvider;
}
public bool CanEdit(YourItem item) {
var u = userProvider.GetCurrentUser();
if (u == null)
return false;
return item.Owner == u && u.IsInRole("EditYourItem");
}
}
/* Controller */
public class MyController: Controller {
IAuthorisationService authorisation;
public MyController(IAuthorisationService authorisation) {
this.authorisation = authorisation;
}
public ActionResult Edit(int id) {
var item = GetTheItembyIdSomehow();
if (!authorisation.CanEdit(item))
return new HttpUnauthorizedResult();
// Can do this
}
}
는 그런 다음 컨트롤러에 자동으로 필요한 의존성을 주입 ControllerFactory을 사용할 수 있습니다 : 내 경험에서
나는 그것을 컨트롤러에 서비스를 주입하고 모든 행동에 그것을 사용하기 쉽다는 것을 알 수 있습니다
class DependencyInjectionContainer : WindsorContainer {
public DependencyInjectionContainer() {
RegisterDependencies();
}
private void RegisterDependencies() {
// Services
Register(
AllTypes.Of<IDiscoverableService>()
.FromAssembly(typeof(IDiscoverableService).Assembly)
.Configure(c => c.LifeStyle.Transient)
.WithService.FromInterface()
);
// Controllers
Register(
AllTypes.Of<IController>()
.FromAssembly(typeof(DependencyInjectionContainer).Assembly)
.Configure(c => c.LifeStyle.Transient)
);
}
}
class WindsorControllerFactory : DefaultControllerFactory, IDisposable {
private readonly IWindsorContainer container;
public WindsorControllerFactory() {
container = new DependencyInjectionContainer();
}
protected override IController GetControllerInstance(Type controllerType) {
if (controllerType == null)
return base.GetControllerInstance(controllerType);
return (IController) container.Resolve(controllerType);
}
public void Dispose() {
container.Dispose();
}
}
좋습니다. 재 : 권한 테이블, ContentID는 무엇입니까? Orders, OrderItems, SalesSlips 등 ... 많은 엔터티가 있다고 가정 해 보겠습니다. 콘텐츠 란 무엇입니까? ID가 무엇을 매핑했는지 어떻게 알 수 있습니까? – zzz
이 경우 권한 테이블에 TableID 인 다른 필드가 필요합니다. 또한 테이블 이름과 테이블 ID를 포함하는 테이블을 갖는 것이 좋습니다. –
또는 사용자가 액세스 할 수있는 엔티티로 이동하여 해당 레코드를 통합 할 수 있습니다. 예를 들어 영업 사원이 특정 주문에 액세스 할 수있는 경우 해당 주문의 모든 OrderItem에도 액세스 할 수 있습니다. –