Initial assumptions
I assume the following architecture:
Stateless Scaling Sharding . . . . . . +=========================================+ | Service Layer | +----------+ | +-----------+ +---------------+ | +------------+ | | HTTP | | | | | | Driver, Wire, etc. | | | Client | <============> | RESTful | <====> | Data Access | <=======================> | Database | | | JSON | | Service | DTO | Layer | | ORM, Raw, etc. | | | | | | | | | | | | +----------+ | +-----------+ +---------------+ | +------------+ +=========================================+ . . . . . .
First, suppose you authenticate Client using the Service Layer and get a specific token that encodes authentication and authorization information in it.
First of all, I first think about processing all requests, and then filtering them only depending on authorization. This will simplify and simplify the work. However, of course, some queries may require expensive processing, in which case this approach is completely ineffective. High load requests, on the other hand, are likely to be associated with access to a resource, which, as you said, is easily organized and can be detected and resolved in the Service Layer in the API or at least Data Access levels.
Further thoughts
As for authorization at the instance level and property, I will not even try to put it in the Data Access Layer and completely isolate it outside the API level, that is, starting from the Data Access Layer none of the levels will even know about it. Even if you request a list of 1M objects and want to release one or two properties from all objects for this particular client, it would be preferable to extract all the objects, and then only hide the properties.
Another assumption is that your model is a clear DTO , that is, just a data container, and all business logic is implemented at the Service Layer level, especially the API . And say that you are transmitting data via HTTP encoded as JSON . Anyway, somewhere in front of the API layer you will have a small serialization stage to convert your model to JSON . Thus, this stage - this is the place where I think is the ideal place to place an instance and permit property.
Sentence
If it comes to authorization at the property level, I think that there is no reasonable way to isolate the model from security logic. Whether it is rule-based authorization, role-based authorization, or at any other level, this process will be checked for value from the authentication / authorization token provided by Client . So, at the serialization level, you will basically get two parameters, a token and a model, and accordingly serialize the corresponding properties or the instance as a whole.
When it comes to defining rules, roles, and whatevers for a property for a model, this can be done in various ways depending on the available paradigms, that is, depending on the language that the Service Layer will implement. Definitions can be heavily used by Annotations (Java) or Decorators (Python) . For emitting specific properties, Python will be convenient due to its dynamic collections and hacker functions, for example. Descriptors . In the case of Java, you can complete the encapsulation of properties in a template class, such as AuthField<T> .
Summary
To summarize, I would suggest placing instance and property authorization in front of API Layer at the serialization stage. Thus, in principle, roles / rules will be assigned in the model, and authorization will be performed in the serializer, providing the model and token.