소스 검색

总后台添加机构权限功能

whj 4 년 전
부모
커밋
b772fdb86b

+ 1 - 1
app/Admin/Actions/Cdmuser.php

xqd
@@ -11,6 +11,6 @@ class Cdmuser extends RowAction
     public $name = '创建账号';
     public function href()
     {
-        return '/admin/cdms_user?id='.$this->row->id;
+        return '/admin/cdms_user/create?id='.$this->row->id;
     }
 }

+ 8 - 4
app/Admin/Actions/UpdateCdms.php

xqd
@@ -2,16 +2,20 @@
 
 namespace App\Admin\Actions;
 
+use App\Models\Organization;
 use Encore\Admin\Actions\RowAction;
-use Illuminate\Http\Request;
 
-class UpdateCdm extends RowAction
+class UpdateCdms extends RowAction
 {
    public $name = '修改账号';
 
-    public function href(Request $request)
+    public function href()
     {
         $id = $this->row->id;
-        return "/cdms_user/".$id."/edit";
+        $cmds_id = Organization::where(['id'=>$id])->value('cdms_id');
+        if(empty($cmds_id)){
+            return '/admin/organizations';
+        }
+        return "/admin/cdms_user/".$cmds_id."/edit";
     }
 }

+ 14 - 1
app/Admin/Controllers/CdmsController.php

xqd xqd xqd
@@ -4,12 +4,14 @@
 namespace App\Admin\Controllers;
 
 
+use App\Models\Organization;
 use Encore\Admin\Controllers\AdminController;
 use Encore\Admin\Form;
 
 class CdmsController extends AdminController
 {
 
+    protected $title = '机构账号';
     /**
      * Make a form builder.
      *
@@ -21,6 +23,9 @@ class CdmsController extends AdminController
         $permissionModel = config('tenancy.database.permissions_model');
         $roleModel = config('tenancy.database.roles_model');
 
+        $id = request('id');
+//        var_dump(intval($id));
+//        dd('ss');
         $form = new Form(new $userModel());
 
         $userTable = config('tenancy.database.users_table');
@@ -41,18 +46,26 @@ class CdmsController extends AdminController
 
         $form->ignore(['password_confirmation']);
 
+        $form->hidden('org_id')->value(request('id'));
         $form->multipleSelect('roles', trans('tenancy.roles'))->options($roleModel::all()->pluck('name', 'id'));
         $form->multipleSelect('permissions', trans('tenancy.permissions'))->options($permissionModel::all()->pluck('name', 'id'));
 
         $form->display('created_at', trans('tenancy.created_at'));
         $form->display('updated_at', trans('tenancy.updated_at'));
 
-        $form->saving(function (Form $form) {
+        $form->saving(function (Form $form) use ($id){
             if ($form->password && $form->model()->password != $form->password) {
                 $form->password = bcrypt($form->password);
             }
         });
 
+        $form->saved(function (Form $form) use ($id) {
+            $org_id = $form->model()->org_id;
+            \Log::info($form->model()->id.'--------id-----'.'组织id'.$org_id);
+            Organization::where(['id'=>$org_id])->update(['cdms_id'=>$form->model()->id]);
+           return redirect('/admin/organizations');
+        });
+
         return $form;
     }
 }

+ 1 - 1
app/Admin/Controllers/HomeController.php

xqd
@@ -3,7 +3,7 @@
 namespace App\Admin\Controllers;
 
 use App\Http\Controllers\Controller;
-use Encore\Admin\Http\Controllers\Dashboard;
+use Encore\Admin\Controllers\Dashboard;
 use Encore\Admin\Layout\Column;
 use Encore\Admin\Layout\Content;
 use Encore\Admin\Layout\Row;

+ 6 - 1
app/Admin/Controllers/OrganizationController.php

xqd xqd
@@ -3,6 +3,7 @@
 namespace App\Admin\Controllers;
 
 use App\Admin\Actions\Cdmuser;
+use App\Admin\Actions\UpdateCdms;
 use App\Models\Area;
 use App\Models\Organization;
 use Encore\Admin\Controllers\AdminController;
@@ -41,7 +42,11 @@ class OrganizationController extends AdminController
         $grid->column('updated_at', __('更新时间'));
 
         $grid->actions(function ($actions){
-            $actions->add(new Cdmuser());
+            if($actions->row->cdms_id){
+                $actions->add(new UpdateCdms());
+            } else {
+                $actions->add(new Cdmuser());
+            }
         });
 
         return $grid;

+ 1 - 1
app/Admin/routes.php

xqd
@@ -13,7 +13,7 @@ Route::group([
     $router->get('/', 'HomeController@index')->name('home');
     $router->get('/api/getCity', 'ApiController@getCity');
     $router->get('/api/getArea', 'ApiController@getArea');
-    $router->get('/cdms_user', 'CdmsController@form');
+    $router->resource('/cdms_user', CdmsController::class);
     $router->resource('organizations', OrganizationController::class);
 });
 

+ 98 - 0
app/Models/CdmsMenus.php

xqd
@@ -0,0 +1,98 @@
+<?php
+
+namespace App\Models;
+
+use Encore\Admin\Traits\DefaultDatetimeFormat;
+use Encore\Admin\Traits\ModelTree;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Relations\BelongsToMany;
+use Illuminate\Support\Facades\DB;
+
+class CdmsMenus extends Model
+{
+    //
+    use DefaultDatetimeFormat;
+    use ModelTree {
+        ModelTree::boot as treeBoot;
+    }
+
+    /**
+     * The attributes that are mass assignable.
+     *
+     * @var array
+     */
+    protected $fillable = ['parent_id', 'order', 'title', 'icon', 'uri', 'permission'];
+
+    /**
+     * Create a new Eloquent model instance.
+     *
+     * @param array $attributes
+     */
+    public function __construct(array $attributes = [])
+    {
+        $connection = config('tenancy.database.connection') ?: config('database.default');
+
+        $this->setConnection($connection);
+
+        $this->setTable(config('tenancy.database.menu_table'));
+
+        parent::__construct($attributes);
+    }
+
+    /**
+     * A Menu belongs to many roles.
+     *
+     * @return BelongsToMany
+     */
+    public function roles(): BelongsToMany
+    {
+        $pivotTable = config('tenancy.database.role_menu_table');
+
+        $relatedModel = config('tenancy.database.roles_model');
+
+        return $this->belongsToMany($relatedModel, $pivotTable, 'menu_id', 'role_id');
+    }
+
+    /**
+     * @return array
+     */
+    public function allNodes(): array
+    {
+        $connection = config('tenancy.database.connection') ?: config('database.default');
+        $orderColumn = DB::connection($connection)->getQueryGrammar()->wrap($this->orderColumn);
+
+        $byOrder = 'ROOT ASC,'.$orderColumn;
+
+        $query = static::query();
+
+        if (config('tenancy.check_menu_roles') !== false) {
+            $query->with('roles');
+        }
+
+        return $query->selectRaw('*, '.$orderColumn.' ROOT')->orderByRaw($byOrder)->get()->toArray();
+    }
+
+    /**
+     * determine if enable menu bind permission.
+     *
+     * @return bool
+     */
+    public function withPermission()
+    {
+        return (bool) config('tenancy.menu_bind_permission');
+    }
+
+    /**
+     * Detach models from the relationship.
+     *
+     * @return void
+     */
+    protected static function boot()
+    {
+        static::treeBoot();
+
+        static::deleting(function ($model) {
+            $model->roles()->detach();
+        });
+    }
+}

+ 168 - 0
app/Models/CdmsPermissions.php

xqd
@@ -0,0 +1,168 @@
+<?php
+
+namespace App\Models;
+
+use Encore\Admin\Traits\DefaultDatetimeFormat;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Relations\BelongsToMany;
+use Illuminate\Http\Request;
+use Illuminate\Support\Str;
+
+class CdmsPermissions extends Model
+{
+    use DefaultDatetimeFormat;
+
+    /**
+     * @var array
+     */
+    protected $fillable = ['name', 'slug', 'http_method', 'http_path'];
+
+    /**
+     * @var array
+     */
+    public static $httpMethods = [
+        'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD',
+    ];
+
+    /**
+     * Create a new Eloquent model instance.
+     *
+     * @param array $attributes
+     */
+    public function __construct(array $attributes = [])
+    {
+        $connection = config('tenancy.database.connection') ?: config('database.default');
+
+        $this->setConnection($connection);
+
+        $this->setTable(config('tenancy.database.permissions_table'));
+
+        parent::__construct($attributes);
+    }
+
+    /**
+     * Permission belongs to many roles.
+     *
+     * @return BelongsToMany
+     */
+    public function roles(): BelongsToMany
+    {
+        $pivotTable = config('tenancy.database.role_permissions_table');
+
+        $relatedModel = config('tenancy.database.roles_model');
+
+        return $this->belongsToMany($relatedModel, $pivotTable, 'permission_id', 'role_id');
+    }
+
+    /**
+     * If request should pass through the current permission.
+     *
+     * @param Request $request
+     *
+     * @return bool
+     */
+    public function shouldPassThrough(Request $request): bool
+    {
+        if (empty($this->http_method) && empty($this->http_path)) {
+            return true;
+        }
+
+        $method = $this->http_method;
+
+        $matches = array_map(function ($path) use ($method) {
+            $path = trim(config('tenancy.route.prefix'), '/').$path;
+
+            if (Str::contains($path, ':')) {
+                list($method, $path) = explode(':', $path);
+                $method = explode(',', $method);
+            }
+
+            return compact('method', 'path');
+        }, explode("\n", $this->http_path));
+
+        foreach ($matches as $match) {
+            if ($this->matchRequest($match, $request)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    /**
+     * filter \r.
+     *
+     * @param string $path
+     *
+     * @return mixed
+     */
+    public function getHttpPathAttribute($path)
+    {
+        return str_replace("\r\n", "\n", $path);
+    }
+
+    /**
+     * If a request match the specific HTTP method and path.
+     *
+     * @param array   $match
+     * @param Request $request
+     *
+     * @return bool
+     */
+    protected function matchRequest(array $match, Request $request): bool
+    {
+        if ($match['path'] == '/') {
+            $path = '/';
+        } else {
+            $path = trim($match['path'], '/');
+        }
+
+        if (!$request->is($path)) {
+            return false;
+        }
+
+        $method = collect($match['method'])->filter()->map(function ($method) {
+            return strtoupper($method);
+        });
+
+        return $method->isEmpty() || $method->contains($request->method());
+    }
+
+    /**
+     * @param $method
+     */
+    public function setHttpMethodAttribute($method)
+    {
+        if (is_array($method)) {
+            $this->attributes['http_method'] = implode(',', $method);
+        }
+    }
+
+    /**
+     * @param $method
+     *
+     * @return array
+     */
+    public function getHttpMethodAttribute($method)
+    {
+        if (is_string($method)) {
+            return array_filter(explode(',', $method));
+        }
+
+        return $method;
+    }
+
+    /**
+     * Detach models from the relationship.
+     *
+     * @return void
+     */
+    protected static function boot()
+    {
+        parent::boot();
+
+        static::deleting(function ($model) {
+            $model->roles()->detach();
+        });
+    }
+}

+ 112 - 0
app/Models/CdmsRoles.php

xqd
@@ -0,0 +1,112 @@
+<?php
+
+namespace App\Models;
+
+use Encore\Admin\Traits\DefaultDatetimeFormat;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Relations\BelongsToMany;
+
+class CdmsRoles extends Model
+{
+    use DefaultDatetimeFormat;
+
+    protected $fillable = ['name', 'slug'];
+
+    /**
+     * Create a new Eloquent model instance.
+     *
+     * @param array $attributes
+     */
+    public function __construct(array $attributes = [])
+    {
+        $connection = config('tenancy.database.connection') ?: config('database.default');
+
+        $this->setConnection($connection);
+
+        $this->setTable(config('tenancy.database.roles_table'));
+
+        parent::__construct($attributes);
+    }
+
+    /**
+     * A role belongs to many users.
+     *
+     * @return BelongsToMany
+     */
+    public function administrators(): BelongsToMany
+    {
+        $pivotTable = config('tenancy.database.role_users_table');
+
+        $relatedModel = config('tenancy.database.users_model');
+
+        return $this->belongsToMany($relatedModel, $pivotTable, 'role_id', 'user_id');
+    }
+
+    /**
+     * A role belongs to many permissions.
+     *
+     * @return BelongsToMany
+     */
+    public function permissions(): BelongsToMany
+    {
+        $pivotTable = config('tenancy.database.role_permissions_table');
+
+        $relatedModel = config('tenancy.database.permissions_model');
+
+        return $this->belongsToMany($relatedModel, $pivotTable, 'role_id', 'permission_id');
+    }
+
+    /**
+     * A role belongs to many menus.
+     *
+     * @return BelongsToMany
+     */
+    public function menus(): BelongsToMany
+    {
+        $pivotTable = config('tenancy.database.role_menu_table');
+
+        $relatedModel = config('tenancy.database.menu_model');
+
+        return $this->belongsToMany($relatedModel, $pivotTable, 'role_id', 'menu_id');
+    }
+
+    /**
+     * Check user has permission.
+     *
+     * @param $permission
+     *
+     * @return bool
+     */
+    public function can(string $permission): bool
+    {
+        return $this->permissions()->where('slug', $permission)->exists();
+    }
+
+    /**
+     * Check user has no permission.
+     *
+     * @param $permission
+     *
+     * @return bool
+     */
+    public function cannot(string $permission): bool
+    {
+        return !$this->can($permission);
+    }
+
+    /**
+     * Detach models from the relationship.
+     *
+     * @return void
+     */
+    protected static function boot()
+    {
+        parent::boot();
+
+        static::deleting(function ($model) {
+            $model->administrators()->detach();
+
+            $model->permissions()->detach();
+        });
+    }
+}

+ 37 - 0
app/Models/CdmsUsers.php

xqd
@@ -0,0 +1,37 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Relations\BelongsToMany;
+
+class CdmsUsers extends Model
+{
+    /**
+     * A user has and belongs to many roles.
+     *
+     * @return BelongsToMany
+     */
+    public function roles(): BelongsToMany
+    {
+        $pivotTable = config('tenancy.database.role_users_table');
+
+        $relatedModel = config('tenancy.database.roles_model');
+
+        return $this->belongsToMany($relatedModel, $pivotTable, 'user_id', 'role_id');
+    }
+
+    /**
+     * A User has and belongs to many permissions.
+     *
+     * @return BelongsToMany
+     */
+    public function permissions(): BelongsToMany
+    {
+        $pivotTable = config('tenancy.database.user_permissions_table');
+
+        $relatedModel = config('tenancy.database.permissions_model');
+
+        return $this->belongsToMany($relatedModel, $pivotTable, 'user_id', 'permission_id');
+    }
+}

+ 5 - 0
app/Models/Organization.php

xqd
@@ -26,4 +26,9 @@ class Organization extends BaseModel
     {
         return $this->belongsToMany(Docter::class);
     }
+
+    public function user()
+    {
+        return $this->hasOne(CdmsUsers::class,'org_id','id');
+    }
 }

+ 4 - 4
config/tenancy.php

xqd
@@ -153,19 +153,19 @@ return [
 
         // User tables and model.
         'users_table' => 'cdms_users',
-        'users_model' => Encore\Admin\Auth\Database\Administrator::class,
+        'users_model' =>\App\Models\CdmsUsers::class,
 
         // Role table and model.
         'roles_table' => 'cdms_roles',
-        'roles_model' => Encore\Admin\Auth\Database\Role::class,
+        'roles_model' => \App\Models\CdmsRoles::class,
 
         // Permission table and model.
         'permissions_table' => 'cdms_permissions',
-        'permissions_model' => Encore\Admin\Auth\Database\Permission::class,
+        'permissions_model' => \App\Models\CdmsPermissions::class,
 
         // Menu table and model.
         'menu_table' => 'cdms_menu',
-        'menu_model' => Encore\Admin\Auth\Database\Menu::class,
+        'menu_model' => \App\Models\CdmsMenus::class,
 
         // Pivot table for table above.
         'operation_log_table'    => 'cdms_operation_log',

+ 115 - 0
resources/lang/zh-CN/tenancy.php

xqd
@@ -0,0 +1,115 @@
+<?php
+
+
+return [
+    'online' => '在线',
+    'login' => '登录',
+    'logout' => '登出',
+    'setting' => '设置',
+    'name' => '名称',
+    'username' => '用户名',
+    'password' => '密码',
+    'password_confirmation' => '确认密码',
+    'remember_me' => '记住我',
+    'user_setting' => '用户设置',
+    'avatar' => '头像',
+    'list' => '列表',
+    'new' => '新增',
+    'create' => '创建',
+    'delete' => '删除',
+    'remove' => '移除',
+    'edit' => '编辑',
+    'continue_editing' => '继续编辑',
+    'continue_creating' => '继续创建',
+    'view' => '查看',
+    'detail' => '详细',
+    'browse' => '浏览',
+    'reset' => '重置',
+    'export' => '导出',
+    'batch_delete' => '批量删除',
+    'save' => '保存',
+    'refresh' => '刷新',
+    'order' => '排序',
+    'expand' => '展开',
+    'collapse' => '收起',
+    'filter' => '筛选',
+    'search' => '搜索',
+    'close' => '关闭',
+    'show' => '显示',
+    'entries' => '条',
+    'captcha' => '验证码',
+    'action' => '操作',
+    'title' => '标题',
+    'description' => '简介',
+    'back' => '返回',
+    'back_to_list' => '返回列表',
+    'submit' => '提交',
+    'menu' => '菜单',
+    'input' => '输入',
+    'succeeded' => '成功',
+    'failed' => '失败',
+    'delete_confirm' => '确认删除?',
+    'delete_succeeded' => '删除成功 !',
+    'delete_failed' => '删除失败 !',
+    'update_succeeded' => '更新成功 !',
+    'save_succeeded' => '保存成功 !',
+    'refresh_succeeded' => '刷新成功 !',
+    'login_successful' => '登录成功 !',
+    'choose' => '选择',
+    'choose_file' => '选择文件',
+    'choose_image' => '选择图片',
+    'more' => '更多',
+    'deny' => '无权访问',
+    'administrator' => '管理员',
+    'roles' => '角色',
+    'permissions' => '权限',
+    'slug' => '标识',
+    'created_at' => '创建时间',
+    'updated_at' => '更新时间',
+    'alert' => '注意',
+    'parent_id' => '父级菜单',
+    'icon' => '图标',
+    'uri' => '路径',
+    'operation_log' => '操作日志',
+    'parent_select_error' => '父级选择错误',
+    'pagination' => [
+        'range' => '从 :first 到 :last ,总共 :total 条',
+    ],
+    'role' => '角色',
+    'permission' => '权限',
+    'route' => '路由',
+    'confirm' => '确认',
+    'cancel' => '取消',
+    'http' => [
+        'method' => 'HTTP方法',
+        'path' => 'HTTP路径',
+    ],
+    'all_methods_if_empty' => '为空默认为所有方法',
+    'all' => '全部',
+    'current_page' => '当前页',
+    'selected_rows' => '选择的行',
+    'upload' => '上传',
+    'new_folder' => '新建文件夹',
+    'time' => '时间',
+    'size' => '大小',
+    'listbox' => [
+        'text_total' => '总共 {0} 项',
+        'text_empty' => '空列表',
+        'filtered' => '{0} / {1}',
+        'filter_clear' => '显示全部',
+        'filter_placeholder' => '过滤',
+    ],
+    'table_items_selected' => '已选择 {n} 项',
+    'menu_titles' => [],
+    'prev' => '上一步',
+    'next' => '下一步',
+    'quick_create' => '快速创建',
+    'all_menus' => '所有菜单',
+    'accessible_routes' => '可访问路由',
+    'visible_menu' => '可见菜单',
+    'accessible_actions' => '可访问操作',
+    'index' => '列表',
+    'store' => '保存',
+    'update' => '更新',
+];
+