Skip to content

Commit 7f8a05c

Browse files
Merge pull request #4 from jianminLee/master
Support localization.
2 parents 7ceac04 + 07a51f6 commit 7f8a05c

13 files changed

+205
-13
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ Finally, add the `ForgetCachedPermissions` class to your `config/nova.php` middl
7272
\Insenseanalytics\LaravelNovaPermission\ForgetCachedPermissions::class,
7373
],
7474
```
75+
Support for localized permission search, you can enter the display name for permission search,If you don't need this feature, you can remove the `PermissionSearchTranslationTrait` trait from the [Using Custom Permission Resource Classes](https://github.com/insenseanalytics/laravel-nova-permission#using-custom-rolepermission-resource-classes).
76+
77+
You can use the artisan command line tool to publish localization files:
78+
79+
```php
80+
php artisan vendor:publish --provider="Insenseanalytics\LaravelNovaPermission\NovaPermissionServiceProvider"
81+
```
7582

7683
## Using Custom Role/Permission Resource Classes
7784

resources/lang/en/navigation.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
'sidebar-label' => 'Roles & Permissions',
5+
];

resources/lang/en/permissions.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
return [
4+
'name' => 'Name',
5+
'display_name' => 'Display Name',
6+
'guard_name' => 'Guard Name',
7+
'created_at' => 'Created at',
8+
'updated_at' => 'Updated at',
9+
];

resources/lang/en/resources.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
return [
4+
'Roles' => 'Roles',
5+
'Role' => 'Role',
6+
'Permissions' => 'Permissions',
7+
'Permission' => 'Permission',
8+
];

resources/lang/en/roles.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
return [
4+
'name' => 'Name',
5+
'guard_name' => 'Guard Name',
6+
'created_at' => 'Created at',
7+
'updated_at' => 'Updated at',
8+
];

resources/views/navigation.blade.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
d="M7 10V7a5 5 0 1 1 10 0v3h2a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-8c0-1.1.9-2 2-2h2zm2 0h6V7a3 3 0 0 0-6 0v3zm-4 2v8h14v-8H5zm7 2a1 1 0 0 1 1 1v2a1 1 0 0 1-2 0v-2a1 1 0 0 1 1-1z"/>
1111
</svg>
1212
<span class="sidebar-label">
13-
Roles & Permissions
13+
@lang('laravel-nova-permission::navigation.sidebar-label')
1414
</span>
1515
</h3>
1616

@@ -24,7 +24,7 @@
2424
resourceName: 'roles'
2525
}
2626
}" class="text-white ml-8 no-underline dim">
27-
Roles
27+
@lang('laravel-nova-permission::resources.Roles')
2828
</router-link>
2929
</li>
3030
@endif
@@ -37,7 +37,7 @@
3737
resourceName: 'permissions'
3838
}
3939
}" class="text-white ml-8 no-underline dim">
40-
Permissions
40+
@lang('laravel-nova-permission::resources.Permissions')
4141
</router-link>
4242
</li>
4343
@endif

src/NovaPermissionServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ class NovaPermissionServiceProvider extends ServiceProvider
1212
public function boot()
1313
{
1414
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'laravel-nova-permission');
15+
$this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'laravel-nova-permission');
16+
17+
$this->publishes([
18+
__DIR__.'/../resources/lang' => resource_path('lang/vendor/laravel-nova-permission'),
19+
], 'laravel-nova-permission-lang');
1520
}
1621

1722
/**

src/Permission.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class Permission extends Resource
99
{
10-
use PermissionResourceTrait;
10+
use PermissionResourceTrait,PermissionSearchTranslationTrait;
1111

1212
/**
1313
* The model the resource corresponds to.
@@ -38,4 +38,25 @@ class Permission extends Resource
3838
* @var bool
3939
*/
4040
public static $displayInNavigation = false;
41+
42+
/**
43+
* Get the displayable label of the resource.
44+
*
45+
* @return string
46+
*/
47+
public static function label()
48+
{
49+
return __('laravel-nova-permission::resources.Permissions');
50+
}
51+
52+
/**
53+
* Get the displayable singular label of the resource.
54+
*
55+
* @return string
56+
*/
57+
public static function singularLabel()
58+
{
59+
return __('laravel-nova-permission::resources.Permission');
60+
}
61+
4162
}

src/PermissionResourceTrait.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Insenseanalytics\LaravelNovaPermission;
44

5+
use Laravel\Nova\Http\Requests\NovaRequest;
56
use Laravel\Nova\Nova;
67
use Laravel\Nova\Fields\ID;
78
use Illuminate\Http\Request;
@@ -40,18 +41,24 @@ public function fields(Request $request)
4041
return [
4142
ID::make()->sortable(),
4243

43-
Text::make('Name', 'name')
44+
Text::make(__('laravel-nova-permission::permissions.name'), 'name')
4445
->rules(['required', 'string', 'max:255'])
4546
->creationRules('unique:' . config('permission.table_names.permissions'))
4647
->updateRules('unique:' . config('permission.table_names.permissions') . ',name,{{resourceId}}'),
4748

48-
Select::make('Guard Name', 'guard_name')
49+
Text::make(__('laravel-nova-permission::permissions.display_name'),function (){
50+
return __('laravel-nova-permission::permissions.display_names.'.$this->name);
51+
})->canSee(function (){
52+
return is_array(__('laravel-nova-permission::permissions.display_names'));
53+
}),
54+
55+
Select::make(__('laravel-nova-permission::permissions.guard_name'), 'guard_name')
4956
->options($guardOptions->toArray())
5057
->rules(['required', Rule::in($guardOptions)]),
5158

52-
DateTime::make('Created At', 'created_at')->exceptOnForms(),
59+
DateTime::make(__('laravel-nova-permission::permissions.created_at'), 'created_at')->exceptOnForms(),
5360

54-
DateTime::make('Updated At', 'updated_at')->exceptOnForms(),
61+
DateTime::make(__('laravel-nova-permission::permissions.updated_at'), 'updated_at')->exceptOnForms(),
5562

5663
BelongsToMany::make($roleResource::label(), 'roles', $roleResource)->searchable(),
5764

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: work
5+
* Date: 2018/12/15
6+
* Time: 10:26
7+
*/
8+
9+
namespace Insenseanalytics\LaravelNovaPermission;
10+
11+
12+
trait PermissionSearchTranslationTrait {
13+
14+
use TranslationHandelTrait;
15+
16+
/**
17+
* Override the applyFilters method,title field translation
18+
*/
19+
public function title() {
20+
21+
return __('laravel-nova-permission::permissions.display_names.'.$this->name);
22+
}
23+
24+
/**
25+
* Rewrite the applySearch method to apply translation field search
26+
*
27+
* @param \Illuminate\Database\Eloquent\Builder $query
28+
* @param string $search
29+
* @return \Illuminate\Database\Eloquent\Builder
30+
*/
31+
protected static function applySearch($query, $search)
32+
{
33+
return $query->where(function ($query) use ($search) {
34+
if (is_numeric($search) && in_array($query->getModel()->getKeyType(), ['int', 'integer'])) {
35+
$query->orWhere($query->getModel()->getQualifiedKeyName(), $search);
36+
}
37+
38+
$model = $query->getModel();
39+
40+
$connectionType = $query->getModel()->getConnection()->getDriverName();
41+
42+
$likeOperator = $connectionType == 'pgsql' ? 'ilike' : 'like';
43+
44+
$trans_search = array_keys(preg_grep("/$search/",array_dot(__('laravel-nova-permission::permissions.display_names'))));
45+
46+
foreach (static::searchableColumns() as $column) {
47+
$qualify_column = $model->qualifyColumn($column);
48+
foreach ($trans_search as $t_search){
49+
$query->orWhere($qualify_column, $likeOperator, '%'.$t_search.'%');
50+
}
51+
$query->orWhere($qualify_column, $likeOperator, '%'.$search.'%');
52+
}
53+
});
54+
}
55+
}

src/Role.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class Role extends Resource
99
{
10-
use RoleResourceTrait;
10+
use RoleResourceTrait, TranslationHandelTrait;
1111

1212
/**
1313
* The model the resource corresponds to.
@@ -38,4 +38,24 @@ class Role extends Resource
3838
* @var bool
3939
*/
4040
public static $displayInNavigation = false;
41+
42+
/**
43+
* Get the displayable label of the resource.
44+
*
45+
* @return string
46+
*/
47+
public static function label()
48+
{
49+
return __('laravel-nova-permission::resources.Roles');
50+
}
51+
52+
/**
53+
* Get the displayable singular label of the resource.
54+
*
55+
* @return string
56+
*/
57+
public static function singularLabel()
58+
{
59+
return __('laravel-nova-permission::resources.Role');
60+
}
4161
}

src/RoleResourceTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ public function fields(Request $request)
4040
return [
4141
ID::make()->sortable(),
4242

43-
Text::make('Name', 'name')
43+
Text::make(__('laravel-nova-permission::roles.name'), 'name')
4444
->rules(['required', 'string', 'max:255'])
4545
->creationRules('unique:' . config('permission.table_names.roles'))
4646
->updateRules('unique:' . config('permission.table_names.roles') . ',name,{{resourceId}}'),
4747

48-
Select::make('Guard Name', 'guard_name')
48+
Select::make(__('laravel-nova-permission::roles.guard_name'), 'guard_name')
4949
->options($guardOptions->toArray())
5050
->rules(['required', Rule::in($guardOptions)]),
5151

52-
DateTime::make('Created At', 'created_at')->exceptOnForms(),
52+
DateTime::make(__('laravel-nova-permission::roles.created_at'), 'created_at')->exceptOnForms(),
5353

54-
DateTime::make('Updated At', 'updated_at')->exceptOnForms(),
54+
DateTime::make(__('laravel-nova-permission::roles.updated_at'), 'updated_at')->exceptOnForms(),
5555

5656
BelongsToMany::make($permissionResource::label(), 'permissions', $permissionResource)->searchable(),
5757

src/TranslationHandelTrait.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: work
5+
* Date: 2018/12/14
6+
* Time: 16:03
7+
*/
8+
9+
namespace Insenseanalytics\LaravelNovaPermission;
10+
11+
12+
use Laravel\Nova\Http\Requests\NovaRequest;
13+
14+
trait TranslationHandelTrait {
15+
16+
/**
17+
* Override the applyFilters method to add the guard_name condition when filtering
18+
*
19+
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
20+
* @param \Illuminate\Database\Eloquent\Builder $query
21+
* @param array $filters
22+
*
23+
* @return \Illuminate\Database\Eloquent\Builder
24+
*/
25+
protected static function applyFilters(NovaRequest $request, $query, array $filters) {
26+
$query = parent::applyFilters($request, $query, $filters);
27+
if ($model = head($request->__memoized)) {
28+
$guard_name = $model->guard_name ?? self::getGuardForModel(get_class($model));
29+
$query->where('guard_name', $guard_name);
30+
}
31+
32+
return $query;
33+
}
34+
35+
/**
36+
* @param string model
37+
*
38+
* @return string|null
39+
*/
40+
public static function getGuardForModel(string $model) {
41+
42+
return collect(config('auth.guards'))
43+
->map(function ($guard) {
44+
return config("auth.providers.{$guard['provider']}.model");
45+
})->search($model);
46+
}
47+
}

0 commit comments

Comments
 (0)