lcabrini

lcabrini

Ash user management - how to create an administrator that can perform basic CRUD operations on users?

So it seems like I’m spamming the forums of late. Sorry about that, there are a few things I’m trying to figure out.

I’m using ash_authentication_phoenix and I’ve got that working pretty much the way I want it now. I have a user resource with username (not email), password and a few other fields such as role. I’m not using tokens at all. Users cannot register themselves. Instead they need to be created by a user who has the admin role.

Now I need a administrator to be able to perform basic CRUD operations on users. This shouldn’t be complicated, but I’m a bit stumped on some of the operations and was hoping I could get a little help along the way to figure it out.

I’ve tried looking for something like an ash_user_management module that would tie in with the authentication one, but I haven’t found one at least. If anybody has developed one or is working on one, please do let me know. I thought this would be a common enough situtation that somebody would have developed something, but maybe the world has moved away from writing business applications?

I’ve got list and details views worked out already, that didn’t present any problem. I suppose I can use register to create a user, even though there will be an active user session, right? That seems to make a lot of sense. I can’t imagine delete would present any problem. But I’m not sure how to deal with update user, since it will be given a password that should be converted to a hashed one. Is that what :register_with_password does? I’m looking through the sources but seem to be a bit lost. Can I use :register_with_password along with the update action or do I have to write a custom action for it?

Now I may just be really tired and my mind is running around in circles, but I began to think about an alternative, but I don’t suppose two resources could share the same database table, right? Because authentication and user management are clearly two very different activities, just that they happen to use the same underlying data. The idea that came to me was if I’m meant to have on user recource for auth and another one for user management? Somehow that also doesn’t quite sound right though.

Marked As Solved

mindok

mindok

What does the create action on your user resource look like? Ours looks like this:

    create :register do
      argument :password, :string, allow_nil?: false
      argument :password_confirmation, :string, allow_nil?: false
      change set_context(%{strategy_name: :password})
      change AshAuthentication.Strategy.Password.HashPasswordChange
    end

The AshAuthentication.Strategy.Password.HashPasswordChange converts passwords into their hashed version.

So you can then either use MyApp.User.create!(%{name: "Frieda", password: "123", password_confirmation: "123"}} and build a basic UI to do that, or be lazy like us and just use the AshAdmin plugin that creates a screen for you that looks something like:

You can also reset passwords in this auto-generated UI.

Also Liked

lcabrini

lcabrini

Thank you! What I was looking for was AshAuthentication.Strategy.Password.HashPasswordChange, but I just couldn’t find it and I had reached a point where I didn’t even really know what I was doing. I was low on sleep and getting more and more frustrated. You have made my morning! Don’t know how to thank you properly.

mindok

mindok

Glad to be able to help!

zachdaniel

zachdaniel

Creator of Ash

I think you should be able to pull that off. We’ve got set ups where some users have passwords and some users sign in with socials and that kind of thing. So you should be able to add a separate create action, like :create_without_password. Then you can create a magic link, and send them a link to a page

Where Next?

Popular in Questions Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31013 112
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

We're in Beta

About us Mission Statement