fix login auto detect menu link, and partial update tchnician role dealer
This commit is contained in:
@@ -82,4 +82,9 @@ class Dealer extends Model
|
||||
{
|
||||
return $this->hasMany(WorkDealerPrice::class)->active();
|
||||
}
|
||||
|
||||
public function roles()
|
||||
{
|
||||
return $this->belongsToMany(Role::class, 'role_dealer');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class Role extends Model
|
||||
{
|
||||
@@ -11,4 +12,19 @@ class Role extends Model
|
||||
protected $fillable = [
|
||||
'name'
|
||||
];
|
||||
|
||||
public function dealers()
|
||||
{
|
||||
return $this->belongsToMany(Dealer::class, 'role_dealer');
|
||||
}
|
||||
|
||||
public function users()
|
||||
{
|
||||
return $this->hasMany(User::class);
|
||||
}
|
||||
|
||||
public function hasDealer($dealerId)
|
||||
{
|
||||
return $this->dealers()->where('dealers.id', $dealerId)->whereNull('dealers.deleted_at')->exists();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,4 +192,113 @@ class User extends Authenticatable
|
||||
->where('month', $month)
|
||||
->first();
|
||||
}
|
||||
|
||||
public function accessibleDealers()
|
||||
{
|
||||
if (!$this->role_id) {
|
||||
return collect();
|
||||
}
|
||||
|
||||
// Load role with dealers
|
||||
if (!$this->relationLoaded('role')) {
|
||||
$this->load('role.dealers');
|
||||
}
|
||||
|
||||
// If user has specific dealer_id, check if role allows access
|
||||
if ($this->dealer_id) {
|
||||
if ($this->role && $this->role->hasDealer($this->dealer_id)) {
|
||||
return Dealer::where('id', $this->dealer_id)->get();
|
||||
}
|
||||
return collect();
|
||||
}
|
||||
|
||||
// If no specific dealer_id, return all dealers accessible by role
|
||||
return $this->role ? $this->role->dealers : collect();
|
||||
}
|
||||
|
||||
public function canAccessDealer($dealerId)
|
||||
{
|
||||
if (!$this->role_id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Load role with dealers
|
||||
if (!$this->relationLoaded('role')) {
|
||||
$this->load('role.dealers');
|
||||
}
|
||||
|
||||
return $this->role && $this->role->hasDealer($dealerId);
|
||||
}
|
||||
|
||||
public function getPrimaryDealer()
|
||||
{
|
||||
if ($this->dealer_id && $this->canAccessDealer($this->dealer_id)) {
|
||||
return $this->dealer;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all accessible menus for a specific role
|
||||
*
|
||||
* @param int $roleId
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public static function getAccessibleMenus($roleId)
|
||||
{
|
||||
return \App\Models\Privilege::join('menus', 'privileges.menu_id', '=', 'menus.id')
|
||||
->where('privileges.role_id', $roleId)
|
||||
->where('privileges.view', 1)
|
||||
->select('menus.*', 'privileges.view', 'privileges.create', 'privileges.update', 'privileges.delete')
|
||||
->orderBy('menus.id')
|
||||
->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get accessible menus for current user
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function getMyAccessibleMenus()
|
||||
{
|
||||
if (!$this->role_id) {
|
||||
return collect();
|
||||
}
|
||||
return self::getAccessibleMenus($this->role_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if user can access specific menu
|
||||
*
|
||||
* @param string $menuLink
|
||||
* @return bool
|
||||
*/
|
||||
public function canAccessMenu($menuLink)
|
||||
{
|
||||
if (!$this->role_id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return \App\Models\Privilege::join('menus', 'privileges.menu_id', '=', 'menus.id')
|
||||
->where('privileges.role_id', $this->role_id)
|
||||
->where('menus.link', $menuLink)
|
||||
->where('privileges.view', 1)
|
||||
->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if role can access specific menu (static method)
|
||||
*
|
||||
* @param int $roleId
|
||||
* @param string $menuLink
|
||||
* @return bool
|
||||
*/
|
||||
public static function roleCanAccessMenu($roleId, $menuLink)
|
||||
{
|
||||
return \App\Models\Privilege::join('menus', 'privileges.menu_id', '=', 'menus.id')
|
||||
->where('privileges.role_id', $roleId)
|
||||
->where('menus.link', $menuLink)
|
||||
->where('privileges.view', 1)
|
||||
->exists();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user