Welcome Guest, Not a member yet? Register   Sign In
Shield - what is the workflow for adding new users from an admin section?
#1

Hi,
I am new to Shield, what is the flow of creating new users, lets say from an admin section of the application? 
Here is my controller function so far:

PHP Code:
public function add_member()
 {
 
$data = [];
 
$users model('UserModel');
 
$session session();

 if (
$this->request->getMethod() == 'post') {
 
$rules = [
 
//'membership_status' =>
 
'firstname' => 'required|min_length[3]|max_length[50]',
 
'lastname' => 'required|min_length[3]|max_length[50]',
 
'prefered_name' => 'min_length[3]|max_length[50]',
 
'gender' => 'required|min_length[3]|max_length[25]',
 
'date_of_birth' => 'required|valid_date',
 
'address_1' => 'required|min_length[3]|max_length[100]',
 
'address_2' => 'required|min_length[3]|max_length[100]',
 
'postal_code' => 'required|min_length[3]|max_length[10]|integer',
 
'city' => 'required|min_length[3]|max_length[50]',
 
'home_phone' => 'min_length[3]|max_length[50]',
 
'work_phone' => 'min_length[3]|max_length[50]',
 
'mobile' => 'required_without[home_phone,work_phone]|min_length[3]|max_length[50]',
 
'consent' => 'min_length[2]|max_length[50]',
 
//'email' => 'required|min_length[6]|max_length[50]|valid_email',
 
];

 if (!
$this->validate($rules)) {
 
$data['validation'] = $this->validator;
 
 
$session->set('Details'$_POST);
 
//dd($_SESSION);
 
} else {
 
$newData = [
 
//'id' => $id,
 
'membership_status' => $this->request->getVar('membership_status'FILTER_SANITIZE_FULL_SPECIAL_CHARS),
 
'firstname' => $this->request->getVar('firstname'FILTER_SANITIZE_FULL_SPECIAL_CHARS),
 
'lastname' => $this->request->getVar('lastname'FILTER_SANITIZE_FULL_SPECIAL_CHARS),
 
'prefered_name' => $this->request->getVar('prefered_name'FILTER_SANITIZE_FULL_SPECIAL_CHARS),
 
'date_joined' => $this->request->getVar('date_joined'),
 
'gender' => $this->request->getVar('gender'FILTER_SANITIZE_FULL_SPECIAL_CHARS),
 
'date_of_birth' => $this->request->getVar('date_of_birth'),
 
'address_1' => $this->request->getVar('address_1'FILTER_SANITIZE_FULL_SPECIAL_CHARS),
 
'address_2' => $this->request->getVar('address_2'FILTER_SANITIZE_FULL_SPECIAL_CHARS),
 
'postal_code' => $this->request->getVar('postal_code'FILTER_SANITIZE_NUMBER_INT),
 
'city' => $this->request->getVar('city'FILTER_SANITIZE_FULL_SPECIAL_CHARS),
 
'home_phone' => $this->request->getVar('home_phone'FILTER_SANITIZE_NUMBER_INT),
 
'work_phone' => $this->request->getVar('work_phone'FILTER_SANITIZE_NUMBER_INT),
 
'mobile' => $this->request->getVar('mobile'FILTER_SANITIZE_NUMBER_INT),
 
'consent' => $this->request->getVar('consent'),
 
//'email' => $this->request->getVar('email', FILTER_SANITIZE_EMAIL)
 
];

 
$user = new User([
 
'username' => NULL,
 
'email'    => $this->request->getVar('email'FILTER_SANITIZE_EMAIL),
 
'password' => $this->request->getVar('email'FILTER_SANITIZE_EMAIL),
 ]);
 
$users->save($user);
 
$newData['id'] = $users->getInsertID();
 
// To get the complete user object with ID, we need to get from the database
 
$user $users->findById($users->getInsertID());

 
// Add to default group
 
$users->addToDefaultGroup($user);

 
$users->save($newData);
 
 
$userEmail['email'] = $user->getEmail();

 
// Send the user an email with the code
 
$email emailer()->setFrom(setting('Email.fromEmail'), setting('Email.fromName') ?? '');
 
$email->setTo($user->email);
 
$email->setSubject(lang('DojoApp.mailSubject'));
 
$email->setMessage($this->view('email/add_new_user_email'$userEmail));

 if (
$email->send(false) === false) {
 
log_message('error'$email->printDebugger(['headers']));

 return 
redirect()->route('login')->with('error'lang('Auth.unableSendEmailToUser', [$user->email]));
 }

 
// Clear the email
 
$email->clear();

 
$session->setFlashdata('success''Successfuly added new member information');
 return 
redirect()->to('/admin/members');
 }
 }
 echo 
view('templates/header');
 echo 
view('admin/left_nav_bar');
 echo 
view('admin/add_member'$data);
 echo 
view('admin/left_nav_bar_closure');
 echo 
view('admin/javascript');
 echo 
view('templates/footer');
 } 

Any help is appreciated.
Reply
#2

Is this what your looking for?

Sheild -> Creating Users
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

This bit is included in the code I have posted, the issue I need more clarification on the entire flow, for example how do I inform the new user of their new account and how to access the application using it, do I send a magic link, or a basic combination of required fields as a password?
Reply
#4

Hi, could you please approve this thread so others can maybe help with this query?
Reply
#5

I guess with this kind of support its clear why CI not taking off as even a request for help is not approved so maybe a kind soul out there would actually...
Reply
#6

It's sad that time has passed. Anyone willing to help?
Reply
#7

(This post was last modified: 09-03-2023, 12:58 PM by datamweb.)

(05-07-2023, 11:16 PM)nzcidev Wrote: This bit is included in the code I have posted, the issue I need more clarification on the entire flow, for example how do I inform the new user of their new account and how to access the application using it, do I send a magic link, or a basic combination of required fields as a password?

After registering the user in the admin panel, the user can login through the magic-link, and you can also provide the possibility of setting a new password according to the user's login through the magic-link.

The point I saw in your code is that you have used email for the password, this is wrong, you can generate a random password:

and see https://github.com/codeigniter4/shield/d...nt-3311090

(05-19-2023, 07:05 PM)nzcidev Wrote: I guess with this kind of support its clear why CI not taking off as even a request for help is not approved so maybe a kind soul out there would actually...

Sorry, it's not possible for admins to publish posts quickly due to a lot of spam. However, it has been a long time since you asked for help, and you are right.

Personally, I don't want to participate in this forum due to the many problems of the forum software. However, we have tried to answer questions as quickly as possible in https://github.com/codeigniter4/shield/discussions.

@nzcidev We are definitely a community that helps each other. If the answer to the question is still not clear, feel free to mention me.
Reply
#8

(05-07-2023, 06:26 PM)nzcidev Wrote: Hi,
I am new to Shield, what is the flow of creating new users, lets say from an admin section of the application? 
Here is my controller function so far:

PHP Code:
public function add_member()
 {
 
$data = [];
 
$users model('UserModel');
 
$session session();

 if (
$this->request->getMethod() == 'post') {
 
$rules = [
 
//'membership_status' =>
 
'firstname' => 'required|min_length[3]|max_length[50]',
 
'lastname' => 'required|min_length[3]|max_length[50]',
 
'prefered_name' => 'min_length[3]|max_length[50]',
 
'gender' => 'required|min_length[3]|max_length[25]',
 
'date_of_birth' => 'required|valid_date',
 
'address_1' => 'required|min_length[3]|max_length[100]',
 
'address_2' => 'required|min_length[3]|max_length[100]',
 
'postal_code' => 'required|min_length[3]|max_length[10]|integer',
 
'city' => 'required|min_length[3]|max_length[50]',
 
'home_phone' => 'min_length[3]|max_length[50]',
 
'work_phone' => 'min_length[3]|max_length[50]',
 
'mobile' => 'required_without[home_phone,work_phone]|min_length[3]|max_length[50]',
 
'consent' => 'min_length[2]|max_length[50]',
 
//'email' => 'required|min_length[6]|max_length[50]|valid_email',
 
];

 if (!
$this->validate($rules)) {
 
$data['validation'] = $this->validator;
 
 
$session->set('Details'$_POST);
 
//dd($_SESSION);
 
} else {
 
$newData = [
 
//'id' => $id,
 
'membership_status' => $this->request->getVar('membership_status'FILTER_SANITIZE_FULL_SPECIAL_CHARS),
 
'firstname' => $this->request->getVar('firstname'FILTER_SANITIZE_FULL_SPECIAL_CHARS),
 
'lastname' => $this->request->getVar('lastname'FILTER_SANITIZE_FULL_SPECIAL_CHARS),
 
'prefered_name' => $this->request->getVar('prefered_name'FILTER_SANITIZE_FULL_SPECIAL_CHARS),
 
'date_joined' => $this->request->getVar('date_joined'),
 
'gender' => $this->request->getVar('gender'FILTER_SANITIZE_FULL_SPECIAL_CHARS),
 
'date_of_birth' => $this->request->getVar('date_of_birth'),
 
'address_1' => $this->request->getVar('address_1'FILTER_SANITIZE_FULL_SPECIAL_CHARS),
 
'address_2' => $this->request->getVar('address_2'FILTER_SANITIZE_FULL_SPECIAL_CHARS),
 
'postal_code' => $this->request->getVar('postal_code'FILTER_SANITIZE_NUMBER_INT),
 
'city' => $this->request->getVar('city'FILTER_SANITIZE_FULL_SPECIAL_CHARS),
 
'home_phone' => $this->request->getVar('home_phone'FILTER_SANITIZE_NUMBER_INT),
 
'work_phone' => $this->request->getVar('work_phone'FILTER_SANITIZE_NUMBER_INT),
 
'mobile' => $this->request->getVar('mobile'FILTER_SANITIZE_NUMBER_INT),
 
'consent' => $this->request->getVar('consent'),
 
//'email' => $this->request->getVar('email', FILTER_SANITIZE_EMAIL)
 
];

 
$user = new User([
 
'username' => NULL,
 
'email'    => $this->request->getVar('email'FILTER_SANITIZE_EMAIL),
 
'password' => $this->request->getVar('email'FILTER_SANITIZE_EMAIL),
 ]);
 
$users->save($user);
 
$newData['id'] = $users->getInsertID();
 
// To get the complete user object with ID, we need to get from the database
 
$user $users->findById($users->getInsertID());

 
// Add to default group
 
$users->addToDefaultGroup($user);

 
$users->save($newData);
 
 
$userEmail['email'] = $user->getEmail();

 
// Send the user an email with the code
 
$email emailer()->setFrom(setting('Email.fromEmail'), setting('Email.fromName') ?? '');
 
$email->setTo($user->email);
 
$email->setSubject(lang('DojoApp.mailSubject'));
 
$email->setMessage($this->view('email/add_new_user_email'$userEmail));

 if (
$email->send(false) === false) {
 
log_message('error'$email->printDebugger(['headers']));

 return 
redirect()->route('login')->with('error'lang('Auth.unableSendEmailToUser', [$user->email]));
 }

 
// Clear the email
 
$email->clear();

 
$session->setFlashdata('success''Successfuly added new member information');
 return 
redirect()->to('/admin/members');
 }
 }
 echo 
view('templates/header');
 echo 
view('admin/left_nav_bar');
 echo 
view('admin/add_member'$data);
 echo 
view('admin/left_nav_bar_closure');
 echo 
view('admin/javascript');
 echo 
view('templates/footer');
 } 

Any help is appreciated.

Hello I am also learn to use and extend shield.
First dit you implant a custom user provider for all the extra fields?
PHP Code:
php spark shield:model UserModel 

Then change the setting in App/Config/Auth on line 398 I think

PHP Code:
public string $userProvider = \App\Models\UserModel::class; 

The model is like this, so you see I have a extra field fullname.
And this field is also in the database with a migration.
PHP Code:
<?php

declare(strict_types=1);

namespace 
App\Models;

use 
CodeIgniter\Shield\Models\UserModel as ShieldUserModel;

class 
UserModel extends ShieldUserModel
{
    protected function initialize(): void
    
{
        parent::initialize();

        $this->allowedFields = [
            ...$this->allowedFields,

            'full_name',
        ];
    }

Reply
#9

(This post was last modified: 09-04-2023, 05:04 AM by datamweb.)

So you don't need to define variable $newData.
PHP Code:
$newData = [
// ...
 
'full_name' => $this->request->getPost('full_name'),
 ]; 

You can proceed as follows:

PHP Code:
$users model('UserModel');
$user = new User([
    'username' => 'foo-bar',
    'email'    => '[email protected]',
    'password' => 'secret plain text password',
    'full_name' => $this->request->getPost('full_name'),
]);
$users->save($user); 
Reply
#10

This has been here awhile... Thought I would also assist...

I provided an answer to this question here:

https://stackoverflow.com/questions/7612...9#76141519

Once they login for the first time, it will also send them a new code. Verifying the code will send them to wherever you have the force_reset redirect set to.

With all of the extra data you are entering into the tables, you can either expand the users table, or create a new table. More information on that below with code to assist.

https://stackoverflow.com/questions/7627...7#76290897
Reply




Theme © iAndrew 2016 - Forum software by © MyBB