2017-01-19 12:59:49 +01:00
|
|
|
using Microsoft.AspNet.Authorization;
|
2019-08-04 11:45:00 +02:00
|
|
|
using Yavsc.Services;
|
2019-01-26 14:23:53 +00:00
|
|
|
using Yavsc.ViewModels.Auth;
|
2017-01-19 12:59:49 +01:00
|
|
|
|
2019-08-04 11:45:00 +02:00
|
|
|
namespace Yavsc.AuthorizationHandlers {
|
|
|
|
|
|
|
|
|
|
public class ViewFileHandler : AuthorizationHandler<ViewRequirement, ViewFileContext> {
|
|
|
|
|
|
|
|
|
|
IFileSystemAuthManager _authManager;
|
|
|
|
|
|
|
|
|
|
public ViewFileHandler (IFileSystemAuthManager authManager) {
|
|
|
|
|
_authManager = authManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Handle (AuthorizationContext context, ViewRequirement requirement, ViewFileContext fileContext) {
|
2017-01-19 12:59:49 +01:00
|
|
|
// TODO file access rules
|
2019-08-04 11:45:00 +02:00
|
|
|
if (fileContext.Path.StartsWith ("/pub/"))
|
|
|
|
|
context.Succeed (requirement);
|
2017-01-19 12:59:49 +01:00
|
|
|
else {
|
2019-08-04 11:45:00 +02:00
|
|
|
if (!fileContext.Path.StartsWith ("/"))
|
|
|
|
|
context.Fail ();
|
|
|
|
|
else {
|
|
|
|
|
var rights = _authManager.GetFilePathAccess (context.User, fileContext.Path.Substring (1));
|
|
|
|
|
if ((rights & FileAccessRight.Read) > 0)
|
|
|
|
|
context.Succeed (requirement);
|
|
|
|
|
else context.Fail ();
|
|
|
|
|
}
|
2017-01-19 12:59:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|