sachatrauwaen / OpenContent

Structured Content editing for DNN (Dotnetnuke)
46 stars 25 forks source link

How to access and save current user #205

Open RichardHowells opened 1 year ago

RichardHowells commented 1 year ago

Full disclosure - I am a novice with this module.

I put together a simple gallery template. It works but is too simplistic.

When an image is added I want to record the currently logged in user and display that information in the display template. I see a function (ModelFactoryMultiple.EnhanceUser) that it seems would make user data available in the hbs template via (say) Context.CreatedByUser.DisplayName, but that displays nothing. Running under the debugger calls the EnhanceUser function, but never gets to the lines of code (around 156-165 that actually access the logged in user data.

What do I need in my template/manifest/other config file to trigger those lines of code? How would I store them in my schema?

Timo-Breumelhof commented 1 year ago

Maybe @sachatrauwaen has a better solution but here's my take on this.

You can get to the user when you use a Razor template, and use a hidden field to store the userid, which you can fill using data manipulation using an edit.js file. Example here:https://github.com/40fingers/OpenContent-Demo-Templates/tree/main/20.02-Manipulate-Submitted-Data

FYI I have not tested this with the VUE2 editor TBH.

RichardHowells commented 1 year ago

@Timo-Breumelhof That's a clever workaround, but does seem quite complex for what I feel should be simple.

I tried a horrible hack. I took most of the conditional code out of ModelFactoryMultiple.EnhanceUser so that it unconditionally populates Context.CreatedByUser. I can't say that it completely solves my use case, but it does pass the currently logged in user down to an hbs template.

I have not thought it through at all, but I am inclined to feel that current user would be a reasonable item to have permanently and automatically available.

Timo-Breumelhof commented 1 year ago

Yes I agree that it could be useful

sachatrauwaen commented 1 year ago

@RichardHowells EnhanceUser is only called for multi item templates likes articles.

Is your template a single item template ?