Modify users data as an admin |
I have installed Shield in my project. As an admin in the backoffice I would like to be able to modify user data if necessary. Can anyone give me an example of how this can be done? I used to write the Validation Rules in the model for every table, but for users there are two tables
Try This lesson here https://shield.codeigniter.com/user_mana...ing_users/
Codeigniter First, Codeigniter Then You!!
yekrinaDigitals
This sql query will give you a list of users, and you can use the user_id to make changes depending on what you want to do, if you need to update data in two tables just run two different update queries. Is that helpful enough or do you need some other examples? I am assuming you're using custom data, I don't know if the fill() option in shield will update custom fileds? Not sure about that.
Code: $users = $this->db->table('users, auth_groups_users,auth_identities') (04-26-2024, 04:56 PM)luckmoshy Wrote: Try This lesson here https://shield.codeigniter.com/user_mana...ing_users/ Hi luckmoshy. Thank you for the link. I am trying to use these examples there. The problem is that I do not know where to put in the UserModel the Validation Rules. The update works fine but if for example I erase the username I do not get an error message when I click save. The update is done even with an empty username! Here is my code for the Controller. Just to mention the Users.php Controller lives into the Admin Module not in the app folder: <?php namespace Admin\Controllers; use App\Controllers\BaseController; use Admin\Models\UserModel; use Codeigniter\Shield\Entities\User; use CodeIgniter\HTTP\ResponseInterface; use CodeIgniter\Exceptions\PageNotFoundException; class Users extends BaseController { private UserModel $model; public function __construct() { $this->model = new UserModel; } public function index() { $users = auth()->getProvider(); $myusers = $users->findAll(); return view("Admin\Views\Users\index",[ "users"=>$myusers ]); } public function show($id) { $session = \Config\Services: ![]() $user = $this->getUserOr404($id); return view("Admin\Views\Users\show", [ "user" => $user ]); } public function edit($id) { $session = \Config\Services: ![]() $session->set('recordmode', 'UPDATE'); $user = $this->getUserOr404($id); return view('Admin\Views\Users\edit', [ "user" => $user ]); } public function update($id) { $users = auth()->getProvider(); $user = $this->getUserOr404($id); $user->fill($this->request->getPost()); if ( ! $user->hasChanged()) { return redirect()->back() ->with("message", "Nothing to update."); } if ($users->save($user)) { return redirect()->to("admin/users") ->with("message", "user updated."); } else { return redirect()->back() ->with("errors", $this->model->errors()) ->withInput(); } } public function delete($id) { $user = $this->getUserOr404($id); if ($this->request->is("post")) { $this->model->delete($id); return redirect()->to("admin") ->with("message", "user deleted."); } return view("user/delete", [ "user" => $user ]); } public function remove($id) { $user = $this->getUserOr404($id); if ($this->request->is("post")) { $this->model->delete($id); return redirect()->to("user") ->with("message", "The record was deleted."); } } private function getUserOr404($id): User { $users = auth()->getProvider(); $user = $users->findById($id); if ($user === null) { throw new PageNotFoundException("User with id $id was not found"); } return $user; } } (04-27-2024, 10:54 AM)xsPurX Wrote: This sql query will give you a list of users, and you can use the user_id to make changes depending on what you want to do, if you need to update data in two tables just run two different update queries. Is that helpful enough or do you need some other examples? I am assuming you're using custom data, I don't know if the fill() option in shield will update custom fileds? Not sure about that.
Can just wrap your edit() function to do an if around it, to reload the form if the validation is false, or go somewhere else if its good. Either that or in your update() whatever your form action is.
Code: $validation = \Config\Services::validation(); |
Welcome Guest, Not a member yet? Register Sign In |