77 lines
2.2 KiB
PHP
77 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
use App\Models\BookStackUserModel;
|
|
use App\Models\BookStackRoleModel;
|
|
|
|
class BookStackUserHelper {
|
|
|
|
protected $userModel;
|
|
protected $roleModel;
|
|
|
|
public function __construct() {
|
|
$this->userModel = new BookStackUserModel();
|
|
$this->roleModel = new BookStackRoleModel();
|
|
}
|
|
|
|
public function createEditUser($data,$preData = null) {
|
|
// This function will handle the creation or editing of a user
|
|
// It will check if the user already exists and update or create accordingly
|
|
// The $data parameter should contain all necessary user information
|
|
// For example: ['name' => 'johndoe', 'email' => 'johndoe@doe.com']
|
|
|
|
if (!empty($preData)) {
|
|
|
|
$isExistingUser = $this->userModel->where('email', $preData['email'])->first();
|
|
|
|
if ($isExistingUser) {
|
|
$id = $isExistingUser['id'];
|
|
$update = $this->userModel->where('id', $id)->set($data)->update();
|
|
return $update;
|
|
}
|
|
}
|
|
|
|
$password = 'password';
|
|
$hashedPassword = ['password' => password_hash($password, PASSWORD_DEFAULT)];
|
|
|
|
$slug = ['slug' => $this->createSlug($data['name'])];
|
|
|
|
// array_push($data,$hashedPassword,$slug);
|
|
|
|
$data['password'] = $hashedPassword['password'];
|
|
$data['slug'] = $slug['slug'];
|
|
|
|
// dd($data);
|
|
$this->userModel->insert($data);
|
|
$userId = $this->userModel->insertID();
|
|
|
|
if (!empty($userId)) {
|
|
|
|
$roleData = [
|
|
'user_id' => $userId,
|
|
'role_id' => 3 // 3 is the default role ID for viewers
|
|
];
|
|
|
|
$this->roleModel->insert($roleData);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
protected function createSlug($string) {
|
|
return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $string)));
|
|
}
|
|
|
|
public function deleteUser($data) {
|
|
|
|
$isExistingUser = $this->userModel->where('email', $data['email'])->first();
|
|
if ($isExistingUser) {
|
|
$id = $isExistingUser['id'];
|
|
$delete = $this->userModel->where('id', $id)->delete();
|
|
$this->roleModel->where('user_id', $id)->delete();
|
|
return $delete;
|
|
}
|
|
|
|
}
|
|
} |