views:

128

answers:

2

I have a model containing items, which has many different fields. There is another model which assigns a set of this field to each user using a m2m-relation.

I want to achieve, that in the end, every user has access to a defined set of fields of the item model, and he only sees these field in views, he can only edit these field etc. Is there any generic way to set this up?

A: 

One way to do this would be to break the Item model up into the parts that are individually assignable to a user. If you have fixed user types (admin, customer, team etc.) who can always see the same set of fields, these parts would be whole groups of fields. If it's very dynamic and you want to be able to set up individual fields for each user, each field is a part of its own.

That way, you would have a meta-Item which consists solely of an Id that the parts can refer to. This holds together the parts. Then, you would map a user not to the Item but to the parts and reconstruct the item view from the common Id of the parts.

Hanno Fietz
Sorry for delay and thanks for the answer. The scenario is rather simple: The item-Model has a huge bunch of possible fields, and each user shall only get access to a small set of these. I think that this must be implemented in die model (instead of the view) to generalize it. How would you build a filter like this?
schneck
A: 

A second approach would be to not include the filtering in the model layer. I. e., you leave the mapping on the model layer as it is and retrieve the full set of item fields for each user. Then you pass the items through a filter that implements the rules.

Which approach is better for you depends on how you want to filter. If it's fixed types of users, I would probably implement a rules-based post-processor, if it's very dynamic, I would suggest the approach from my earlier answer. Another reason to put the filtering rules in the model would be if you want to reuse the model in applications that couldn't reuse your filter engine (for example if you have applications in different languages sharing the same database).

Hanno Fietz