Customizing content part

Topics: Customizing Orchard
Feb 19, 2015 at 10:45 AM
Hey folks,

Can someone help me think in the best way to develop the following platform:

So, I have a content part named ProductTrade (this allows users to trade products between them), I created the part and the part record that accept 2 users and 2 collections of products, so far so good... My problem is at the time of creating/editing, for each user they should see different views (with different options), even the display view should be different for anonymous/authenticated/owner. (the editing should occur on the front-end side)

My best guess was to somehow override the controller for edit/create/display actions and create the rules myself. I wanted to avoid writing business logic in the view as well... Or is the driver the best approach?

Can, please, someone help me with this, and if possible provide me some samples of how to achieve it.

Thanks,
João
Feb 20, 2015 at 2:35 PM
Edited Feb 20, 2015 at 2:37 PM
You can access roles in your view:
@{
    var roles = (WorkContext.CurrentUser == null) ? null : ((ContentItem)WorkContext.CurrentUser.ContentItem).As<UserRolesPart>().Roles;
}
Feb 21, 2015 at 11:58 AM
sanderg wrote:
You can access roles in your view:
@{
    var roles = (WorkContext.CurrentUser == null) ? null : ((ContentItem)WorkContext.CurrentUser.ContentItem).As<UserRolesPart>().Roles;
}
Thanks for your answer!

I was trying to avoid business logic in the view, I was expecting it would be possible to do in the controller somehow...
Developer
Feb 21, 2015 at 12:15 PM
You could set a Roles property on your shape from your driver of course.
Feb 23, 2015 at 6:00 PM
sfmskywalker wrote:
You could set a Roles property on your shape from your driver of course.
Can you, please, provide me some sample.

Thanks!
Feb 24, 2015 at 3:09 PM
Edited Feb 24, 2015 at 3:15 PM
When calling the Display method of your driver:
var roles = (WorkContext.CurrentUser == null) ? new List<string>() : ((ContentItem)WorkContext.CurrentUser.ContentItem).As<UserRolesPart>().Roles;

return ContentShape("Parts_ProductTrade", () => shapeHelper.Parts_ProductTrade(
                MyProperty: aProperty,
                AnotherProperty: someOtherProperty,
                Roles: roles));
This way the business logic is handled in the driver, and the Roles are available on the Model in your parts/ProductTrade.cshtml:
@{
    var roles = Model.Roles;
    var isAdmin = roles.Contains("Admin"); // check for specific role, could also be done in the driver
}