Commit 7ba1e24f authored by Viral Solani's avatar Viral Solani

Refactor User Module - Move db queries

parent 7f7092d0
......@@ -2,18 +2,19 @@
namespace App\Http\Controllers\Backend\Access\User;
use App\Models\Access\User\User;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use App\Http\Requests\Backend\Access\User\CreateUserRequest;
use App\Http\Requests\Backend\Access\User\DeleteUserRequest;
use App\Models\Access\Permission\Permission;
use App\Repositories\Backend\Access\Role\RoleRepository;
use App\Repositories\Backend\Access\User\UserRepository;
use App\Http\Requests\Backend\Access\User\EditUserRequest;
use App\Http\Requests\Backend\Access\User\ManageUserRequest;
use App\Http\Requests\Backend\Access\User\ShowUserRequest;
use App\Http\Requests\Backend\Access\User\StoreUserRequest;
use App\Http\Requests\Backend\Access\User\CreateUserRequest;
use App\Http\Requests\Backend\Access\User\DeleteUserRequest;
use App\Http\Requests\Backend\Access\User\ManageUserRequest;
use App\Http\Requests\Backend\Access\User\UpdateUserRequest;
use App\Models\Access\User\User;
use App\Repositories\Backend\Access\Role\RoleRepository;
use App\Repositories\Backend\Access\User\UserRepository;
use Illuminate\Support\Facades\DB;
/**
* Class UserController.
......@@ -94,15 +95,16 @@ class UserController extends Controller
*/
public function edit(User $user, EditUserRequest $request)
{
$userPermissions = DB::table('permission_user')->where('user_id', $user->id)->pluck('permission_id', 'permission_id')->toArray();
$permissions = DB::table('permissions')->pluck('display_name', 'id')->toArray();
ksort($userPermissions);
ksort($permissions);
return view('backend.access.users.edit', compact('userPermissions', 'permissions'))
->withUser($user)
->withUserRoles($user->roles->pluck('id')->all())
->withRoles($this->roles->getAll());
$permissions = Permission::getSelectData('display_name');
$userPermissions = $user->permissions()->get()->pluck('id')->toArray();
return view('backend.access.users.edit')->with([
'user' => $user,
'userRoles'=> $user->roles->pluck('id')->all(),
'roles' => $this->roles->getAll(),
'userPermissions' => $userPermissions ,
'permissions' => $permissions
]);
}
/**
......
......@@ -14,11 +14,11 @@ class BaseModel extends Model
*
* @return array
*/
public static function getSelectData($id = null, $val = null)
public static function getSelectData($field_name = 'name')
{
$collection = parent::all();
return self::getItems($collection);
return self::getItems($collection, $field_name);
}
/**
......@@ -28,24 +28,20 @@ class BaseModel extends Model
*
* @return array
*/
public static function getItems($collection)
public static function getItems($collection,$field_name)
{
$items = [];
foreach ($collection as $model) {
$items[$model->id] = [
'id' => $model->id,
'name' => $model->name,
'name' => $model->$field_name,
'model' => $model,
];
}
foreach ($items as $id => $item) {
if (isset(static::$selectHTMLFormat) && static::$selectHTMLFormat !== '') {
$items[$item['id']] = static::generateSelectName($item['model'], static::$selectHTMLFormat);
} else {
$items[$item['id']] = $item['name'];
}
}
return $items;
......
......@@ -28,50 +28,22 @@ class UserRepository extends BaseRepository
const MODEL = User::class;
/**
* @var RoleRepository
* @var User Model
*/
protected $role;
protected $model;
/**
* @param RoleRepository $role
*/
public function __construct(RoleRepository $role)
{
$this->role = $role;
}
/**
* @param $permissions
* @param string $by
*
* @return mixed
* @var RoleRepository
*/
public function getByPermission($permissions, $by = 'name')
{
if (!is_array($permissions)) {
$permissions = [$permissions];
}
return $this->query()->whereHas('roles.permissions', function ($query) use ($permissions, $by) {
$query->whereIn('permissions.'.$by, $permissions);
})->get();
}
protected $role;
/**
* @param $roles
* @param string $by
*
* @return mixed
* @param RoleRepository $role
*/
public function getByRole($roles, $by = 'name')
public function __construct(User $model, RoleRepository $role)
{
if (!is_array($roles)) {
$roles = [$roles];
}
return $this->query()->whereHas('roles', function ($query) use ($roles, $by) {
$query->whereIn('roles.'.$by, $roles);
})->get();
$this->model = $model;
$this->role = $role;
}
/**
......@@ -162,7 +134,7 @@ class UserRepository extends BaseRepository
*
* @return bool
*/
public function update(Model $user, $request)
public function update($user, $request)
{
$data = $request->except('assignees_roles', 'permissions');
$roles = $request->get('assignees_roles');
......@@ -406,4 +378,38 @@ class UserRepository extends BaseRepository
return $user;
}
/**
* @param $permissions
* @param string $by
*
* @return mixed
*/
public function getByPermission($permissions, $by = 'name')
{
if (!is_array($permissions)) {
$permissions = [$permissions];
}
return $this->query()->whereHas('roles.permissions', function ($query) use ($permissions, $by) {
$query->whereIn('permissions.'.$by, $permissions);
})->get();
}
/**
* @param $roles
* @param string $by
*
* @return mixed
*/
public function getByRole($roles, $by = 'name')
{
if (!is_array($roles)) {
$roles = [$roles];
}
return $this->query()->whereHas('roles', function ($query) use ($roles, $by) {
$query->whereIn('roles.'.$by, $roles);
})->get();
}
}
......@@ -18,12 +18,6 @@ class CreateUsersTable extends Migration
$table->string('last_name', 191);
$table->string('email', 191)->unique();
$table->string('password', 191)->nullable();
$table->text('address', 65535)->nullable();
$table->integer('country_id')->unsigned()->index();
$table->integer('state_id')->unsigned()->index();
$table->integer('city_id')->unsigned()->index();
$table->string('zip_code', 191);
$table->string('ssn', 191);
$table->boolean('status')->default(1);
$table->string('confirmation_code', 191)->nullable();
$table->boolean('confirmed')->default(0);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment