vincent15000's avatar

Add email to a group and create it if it not exists

Hello,

I have this situation.

An authenticated user has itw own account and can invite some other users to join his group to collaborate on the same datas.

Each user has his own group (belongsTo) and can be in several groups (belongsToMany).

The group owner wants to invite [email protected], so he types [email protected] in the input field and clicks on the add button.

When he clicks on the add button, it calls the method add in a controller. But this method has only the purpose of adding the user to the owner group.

I need this scenario :

  • click on the add button

  • check if the user exists

  • if he doesn't exist, create it and add it to the owner group

  • else if he already exists, add it to the owner group

I'm confused on how to write this in the code. Well the code isn't a problem, I know how to write it, but I mean where do I have to write it, I don't succeed imagine the logic, which method in which controller, ...

I already did such manipulations, I had written the different actions in the same method, but is it really the better way ? I mean the code to create a user should in only one method. And the code to add an existing user to a group should also be in only one method.

Can you help me please ?

Thank you very much.

Vincent

0 likes
28 replies
Sinnbeck's avatar

Not really sure what the question is here? I would just create an action that handles it. The logic sound pretty simple

1 like
vincent15000's avatar

@Sinnbeck Well I have thought about this solution.

I create an add method in the GroupController to add the user to the owner group, but if the user doesn't exist, I have to create it => where ? in the add method ? it's just not the right place because there is already a method to create a new user.

So my question is : if I want a clean code with not the same code at different places, how can I call the creation of the user from the add method ?

vincent15000's avatar

@Sinnbeck You would create an action ? What is the real difference between actions and controllers ? I think there is no difference, it's just another way to organize the code (I had read this in some documentation).

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@vincent15000 yeah totally. But an action can be tested by itself and can be reused. Say you later want to add a command that does the same. You just reuse the action

1 like
vincent15000's avatar

@Sinnbeck This is the database tables.

users : id, name
groups : id, name, user_id
user_group : user_id, group_id

Each user has automatically his own group and can invite some other users to work with him by adding them his own group.

But he can also be added to the group of other users.

Sinnbeck's avatar

@vincent15000 so the code is 3 steps?

  1. Find user by email
  2. Check if group id is in group pivot table
  3. If not add it
1 like
vincent15000's avatar

@Sinnbeck Plus one more step : check if the email exists in the database. That is to allow a boss to add collaborators in his group even if the collaborators don't have any account on the app (in this case the app creates the needed accounts and send an email with a temporary password to the collaborators).

And this is especially this step which disturbs me.

Sinnbeck's avatar

@vincent15000 ah ok I assumed they had to exist already. So users don't require anything but a name? Here I would have an action to handle this. CreateOrFirstUserByEmail::class

1 like
vincent15000's avatar

@Sinnbeck Users require a name, an email and a password. After being created by the app, the users receive an email and then they can connect to work with the host (group owner).

Sinnbeck's avatar

@vincent15000 well sounds like their name will be undefined. Otherwise you need to add a flow. Say a validation if the user exists. If not you show a validation error with an action button that takes them to the create user page with the email pre filled

1 like
vincent15000's avatar

@Sinnbeck And if I want to create it without the creation form ? Only with the email (the name will be the email too, and the password will be generated).

vincent15000's avatar

@Sinnbeck Oh you gave me an idea ... the best way is perhaps to check if the user exists or not. If he doesn't exist, he will receive an email which invite him to create an account and he will be automatically attached to the owner group once his account created.

Sinnbeck's avatar

@vincent15000 yeah you can easily just set the name to some dummy text until they click a link to a form where they can change it. Maybe just an empty string

1 like
vincent15000's avatar

@Sinnbeck But the initial purpose was to create the non existing user accounts without having to invite them to create themselves an account. It's for internal society when a boss add collaborators to the app.

Sinnbeck's avatar

@vincent15000 well someone needs to add a name I assume? You don't want it to be the boss, but not the user either? Not quite sure what you mean then?

1 like
vincent15000's avatar

@Sinnbeck While creating automatically a user just with his email address, the name will automatically set with the email value and the password will be generated with a random string.

Sinnbeck's avatar

@vincent15000 yeah I get that. But who do you want to correct the name afterwards?

3 ideas

  1. After creation, boss is sent to the user profile to fix it
  2. User gets an invite to change name
  3. User just gets asked to change it on first login
1 like
vincent15000's avatar

@Sinnbeck Yes that's all right for me.

How can I manage the creation of the non existing users ? I mean I'd like to have a unique place where the creation of the user is coded, for me it's Fortify. I could write another code to create a new user, but this code already exists in Fortify.

Is it possible to call the Fortify create method from another controller ? Is it a good practice to do so ?

Sinnbeck's avatar

@vincent15000 if there is an action, then you can reuse that. If it's a controller, extract it to somewhere and reuse it. It can also just me a method on the user model

1 like
Sinnbeck's avatar

@vincent15000 as long as it does not validate (I don't remember the code even though I use it myself)

1 like
vincent15000's avatar

@Sinnbeck It validates, but I can manually add some more properties to the request before calling the method.

Sinnbeck's avatar

@vincent15000 What class is it? It sounds a bit like you are trying to work around the issue instead of making a proper implementation :)

1 like
vincent15000's avatar

@Sinnbeck But if you create a new one, you will have two codes for the same action (create a new user) in your app. Why is it a good idea ?

Sinnbeck's avatar

@vincent15000 they don't do the same? One validates that the data from the frontend is correct and unique and creates a user. The other takes very specific data and either find or creates a new user.

1 like

Please or to participate in this conversation.