Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move auth to controllers from repository #1568

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/Webkul/Admin/src/Bouncer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function getAuthorizedUserIds()
}

if ($user->view_permission == 'group') {
return app(UserRepository::class)->getCurrentUserGroupsUserIds();
return app(UserRepository::class)->getGroupsUserIds(auth()->guard('user')->user()->group_ids);
} else {
return [$user->id];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,15 @@ public function store()
'reply' => 'required',
]);

$user = auth()->guard('user')->user();

Event::dispatch('email.create.before');

$email = $this->emailRepository->create(request()->all());
$email = $this->emailRepository->create([
...request()->all(),
'name' => $user->name,
'user_id' => $user->id,
]);

if (! request('is_draft')) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,13 @@ public function destroy(int $id): JsonResponse
*/
public function lookup($lookup): JsonResponse
{
$results = $this->attributeRepository->getLookUpOptions($lookup, request()->input('query'));
$user = auth()->guard('user')->user();

$results = $this->attributeRepository->getLookUpOptions($lookup, [
'query' => request()->input('query'),
'user_id' => $user->id,
'view_permission' => $user->view_permission,
]);

return response()->json($results);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<div class="form-group" style="margin-top: 5px;">
@php
$user = auth()->guard('user')->user();

$options = $attribute->lookup_type
? app('Webkul\Attribute\Repositories\AttributeRepository')->getLookUpOptions($attribute->lookup_type)
? app('Webkul\Attribute\Repositories\AttributeRepository')->getLookUpOptions($attribute->lookup_type, [
'query' => '',
'user_id' => $user->id,
'view_permission' => $user->view_permission,
])
: $attribute->options()->orderBy('sort_order')->get();

$selectedOption = old($attribute->code) ?: $value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
@php
$user = auth()->guard('user')->user();

$options = $attribute->lookup_type
? app('Webkul\Attribute\Repositories\AttributeRepository')->getLookUpOptions($attribute->lookup_type)
? app('Webkul\Attribute\Repositories\AttributeRepository')->getLookUpOptions($attribute->lookup_type, [
'query' => '',
'user_id' => $user->id,
'view_permission' => $user->view_permission,
])
: $attribute->options()->orderBy('sort_order')->get();

$selectedOption = old($attribute->code) ?: $value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
@php
$user = auth()->guard('user')->user();

$options = $attribute->lookup_type
? app('Webkul\Attribute\Repositories\AttributeRepository')->getLookUpOptions($attribute->lookup_type)
? app('Webkul\Attribute\Repositories\AttributeRepository')->getLookUpOptions($attribute->lookup_type, [
'query' => '',
'user_id' => $user->id,
'view_permission' => $user->view_permission,
])
: $attribute->options()->orderBy('sort_order')->get();
@endphp

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
@php
$user = auth()->guard('user')->user();

$options = $attribute->lookup_type
? app('Webkul\Attribute\Repositories\AttributeRepository')->getLookUpOptions($attribute->lookup_type)
? app('Webkul\Attribute\Repositories\AttributeRepository')->getLookUpOptions($attribute->lookup_type, [
'query' => '',
'user_id' => $user->id,
'view_permission' => $user->view_permission,
])
: $attribute->options()->orderBy('sort_order')->get();

$selectedOption = old($attribute->code) ?: $value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
@php
$user = auth()->guard('user')->user();

$options = $attribute->lookup_type
? app('Webkul\Attribute\Repositories\AttributeRepository')->getLookUpOptions($attribute->lookup_type)
? app('Webkul\Attribute\Repositories\AttributeRepository')->getLookUpOptions($attribute->lookup_type, [
'query' => '',
'user_id' => $user->id,
'view_permission' => $user->view_permission,
])
: $attribute->options()->orderBy('sort_order')->get();

$selectedOption = old($attribute->code) ?: $value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
@php
$user = auth()->guard('user')->user();

$options = $attribute->lookup_type
? app('Webkul\Attribute\Repositories\AttributeRepository')->getLookUpOptions($attribute->lookup_type)
? app('Webkul\Attribute\Repositories\AttributeRepository')->getLookUpOptions($attribute->lookup_type, [
'query' => '',
'user_id' => $user->id,
'view_permission' => $user->view_permission,
])
: $attribute->options()->orderBy('sort_order')->get();
@endphp

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function getAttributeByCode($code)
* @param array $columns
* @return array
*/
public function getLookUpOptions($lookup, $query = '', $columns = [])
public function getLookUpOptions($lookup, array $params, $columns = [])
{
$lookup = config('attribute_lookups.'.$lookup);

Expand All @@ -126,21 +126,19 @@ public function getLookUpOptions($lookup, $query = '', $columns = [])
if (Str::contains($lookup['repository'], 'UserRepository')) {
$userRepository = app($lookup['repository'])->where('status', 1);

$currentUser = auth()->guard('user')->user();

if ($currentUser->view_permission === 'group') {
if ($params['view_permission'] === 'group') {
return $userRepository->leftJoin('user_groups', 'users.id', '=', 'user_groups.user_id')
->where('users.name', 'like', '%'.urldecode($query).'%')
->where('users.name', 'like', '%'.urldecode($params['query']).'%')
->get();
} elseif ($currentUser->view_permission === 'individual') {
return $userRepository->where('users.id', $currentUser->id);
} elseif ($params['view_permission'] === 'individual') {
return $userRepository->where('users.id', $params['user_id']);
}

return $userRepository->where('users.name', 'like', '%'.urldecode($query).'%')->get();
return $userRepository->where('users.name', 'like', '%'.urldecode($params['query']).'%')->get();
}

return app($lookup['repository'])->findWhere([
[$lookup['label_column'] ?? 'name', 'like', '%'.urldecode($query).'%'],
[$lookup['label_column'] ?? 'name', 'like', '%'.urldecode($params['query']).'%'],
], $columns);
}

Expand Down
8 changes: 7 additions & 1 deletion packages/Webkul/Automation/src/Helpers/Entity/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public function __construct(
*/
public function getAttributes(string $entityType, array $skipAttributes = []): array
{
$user = auth()->guard('user')->user();

$attributes = [
[
'id' => 'title',
Expand Down Expand Up @@ -102,7 +104,11 @@ public function getAttributes(string $entityType, array $skipAttributes = []): a
'type' => 'select',
'name' => 'User',
'lookup_type' => 'users',
'options' => $this->attributeRepository->getLookUpOptions('users'),
'options' => $this->attributeRepository->getLookUpOptions('users', [
'query' => '',
'user_id' => $user->id,
'view_permission' => $user->view_permission,
]),
],
];

Expand Down
2 changes: 0 additions & 2 deletions packages/Webkul/Email/src/Repositories/EmailRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,9 @@ public function create(array $data)
'from' => config('mail.from.address'),
'user_type' => 'admin',
'folders' => isset($data['is_draft']) ? ['draft'] : ['outbox'],
'name' => auth()->guard('user')->user()->name,
'unique_id' => $uniqueId,
'message_id' => $uniqueId,
'reference_ids' => array_merge($referenceIds, [$uniqueId]),
'user_id' => auth()->guard('user')->user()->id,
], $data));

$email = parent::create($data);
Expand Down
18 changes: 9 additions & 9 deletions packages/Webkul/User/src/Repositories/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ public function model()
*
* @return array
*/
public function getCurrentUserGroupsUserIds()
public function getGroupsUserIds(array $groupIds)
{
$userIds = $this->scopeQuery(function ($query) {
return $query->select('users.*')
->leftJoin('user_groups', 'users.id', '=', 'user_groups.user_id')
->leftJoin('groups', 'user_groups.group_id', 'groups.id')
->whereIn('groups.id', auth()->guard('user')->user()->groups()->pluck('id'));
})->get()->pluck('id')->toArray();

return $userIds;
return $this->scopeQuery(fn ($query) => $query->select('users.*')
->leftJoin('user_groups', 'users.id', '=', 'user_groups.user_id')
->leftJoin('groups', 'user_groups.group_id', 'groups.id')
->whereIn('groups.id', $groupIds)
)
->get()
->pluck('id')
->toArray();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
@php
$user = auth()->guard('user')->user();

$params = [
'query' => '',
'user_id' => $user->id,
'view_permission' => $user->view_permission,
];
@endphp

<x-web_form::layouts>
<x-slot:title>
@lang('Form Preview')
Expand Down Expand Up @@ -157,7 +167,7 @@ class="w-full rounded border border-gray-200 px-2.5 py-2 text-sm font-normal tex
>
@php
$options = $parentAttribute->lookup_type
? app('Webkul\Attribute\Repositories\AttributeRepository')->getLookUpOptions($parentAttribute->lookup_type)
? app('Webkul\Attribute\Repositories\AttributeRepository')->getLookUpOptions($parentAttribute->lookup_type, $params)
: $parentAttribute->options()->orderBy('sort_order')->get();
@endphp

Expand Down Expand Up @@ -188,7 +198,7 @@ class="w-full rounded border border-gray-200 px-2.5 py-2 text-sm font-normal tex

@php
$options = $parentAttribute->lookup_type
? app('Webkul\Attribute\Repositories\AttributeRepository')->getLookUpOptions($parentAttribute->lookup_type)
? app('Webkul\Attribute\Repositories\AttributeRepository')->getLookUpOptions($parentAttribute->lookup_type, $params)
: $parentAttribute->options()->orderBy('sort_order')->get();
@endphp

Expand All @@ -211,7 +221,7 @@ class="error-message mt-1 text-xs italic text-red-600"
<div class="checkbox-control">
@php
$options = $parentAttribute->lookup_type
? app('Webkul\Attribute\Repositories\AttributeRepository')->getLookUpOptions($parentAttribute->lookup_type)
? app('Webkul\Attribute\Repositories\AttributeRepository')->getLookUpOptions($parentAttribute->lookup_type, $params)
: $parentAttribute->options()->orderBy('sort_order')->get();
@endphp

Expand Down
Loading