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


Messages In This Thread
Shield - what is the workflow for adding new users from an admin section? - by nzcidev - 05-07-2023, 06:26 PM



Theme © iAndrew 2016 - Forum software by © MyBB