kaffy icon indicating copy to clipboard operation
kaffy copied to clipboard

[FEATURE-REQUEST] Way to access the conn inside form_fields function

Open pjo336 opened this issue 4 years ago • 3 comments

Describe the problem you're proposing to solve Essentially what I want to do is add authorization to different form fields. The current idea is this: There is a way to select user roles and permissions inside kaffy for the User resource. I do not want an admin to be able to alter their own roles or permissions.

So what I would like to do is something like:

def form_fields(conn) do
  entry = Map.get(conn.assigns, :entry)
  if !is_nil(entry) && conn.assigns.current_user.id == entry.id
    [
      roles: %{create: :readonly, update: :readonly}
    ]
  else
    [
      roles: nil # I'd be setting other things but just for example
    ]
  end
end

Describe the solution you'd like conn in the form_fields is easiest. Perhaps there is a cleaner or already implemented way to do this?

Describe alternatives you've considered I currently block from saving or updating with this check in before_insert and before_update, so the actual problem is solved, but this would provide a nicer UI and experience for an admin.

Additional context Really enjoying Kaffy ;)

pjo336 avatar Oct 02 '20 03:10 pjo336

Hey @pjo336 Thanks for taking the time to submit your feedback and I'm glad you're enjoying Kaffy. I agree with you that this will provide a better experience for both the admin and for you as a developer. Work is already being done to include conn everywhere where it makes sense or could be useful. I'll leave this open for the time being since it provides good description and workarounds 👍🏼 Will close once this is done and shipped.

aesmail avatar Oct 06 '20 07:10 aesmail

I was playing around with this in a fork specifically for the form_fields function, I think it makes a lot of sense to add it to a lot of places just for additional usefulness. If you'd like any help I could open a PR with the fork and we could discuss where else to add it.

pjo336 avatar Oct 06 '20 15:10 pjo336

I noticed that conn is not added in the index/1 function. Is this still in the works to add to various places? I am hoping to get the user off the conn when rendering the index view but perhaps there's a different way to do this

roryfahy avatar Aug 25 '23 13:08 roryfahy