Skip to content

Commit 57e6dd0

Browse files
authored
Merge pull request #33 from coralsio/dev
Support end impersonation session
2 parents 0c41278 + 54850a2 commit 57e6dd0

7 files changed

Lines changed: 149 additions & 20 deletions

File tree

Corals/core/Foundation/DataTables/CoralsBuilder.php

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -168,34 +168,33 @@ public function renderBulkActions()
168168
$action = Arr::get($bulk_action, 'action', $bulk_action_key);
169169
$href = Arr::get($bulk_action, 'href', $this->resource_url);
170170
$title = Arr::get($bulk_action, 'modal-title');
171+
$confirmation = !empty($bulk_action['confirmation']) ? ' data-confirmation="' . $bulk_action['confirmation'] . '" ' : '';
171172

172-
$confirmation = "";
173+
if ($this->isExpandableBulkActions()) {
174+
$btnColorClass = Arr::get($bulk_action, 'color-btn-class', 'btn-primary');
173175

174-
if ($bulk_action['confirmation']) {
175-
$confirmation = ' data-confirmation="' . $bulk_action['confirmation'] . '" ';
176+
$class = "btn btn-sm m-1 " . $btnColorClass;
177+
$action_links .= '<a class="' . $class . '" href="' . $href . '" ' . $confirmation . ' data-action="' . $action . '" data-title="' . $title . '" >Bulk ' . $bulk_action['title'] . '</a>';
178+
} else {
179+
$action_links .= '<li><a class="dropdown-item" href="' . $href . '" ' . $confirmation . ' data-action="' . $action . '" data-title="' . $title . '" >' . $bulk_action['title'] . '</a></li>';
176180
}
177-
178-
$action_links .= '<li><a class="dropdown-item" href="' . $href . '" ' . $confirmation . ' data-action="' . $action . '" data-title="' . $title . '" >' . $bulk_action['title'] . '</a></li>';
179181
}
180182

181183
if (empty($action_links)) {
182184
return '';
183185
}
184186

185-
$actions = '
186-
<div class="btn-group bulk_actions" id="bulk_actions_' . $tableId . '" data-table="' . $tableId . '">
187-
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">'
188-
. trans('Corals::labels.actions') .
189-
'
190-
</button>
191-
<ul class="dropdown-menu" role="menu">';
192-
$actions .= $action_links;
193-
194-
$actions .= ' </ul>
195-
</div>';
196-
197-
198-
return $actions;
187+
if ($this->isExpandableBulkActions()) {
188+
return '<div class="bulk-actions d-inline" id="bulk_actions_' . $tableId . '" data-table="' . $tableId . '">' . $action_links . '</div>';
189+
} else {
190+
return '
191+
<div class="btn-group bulk_actions" id="bulk_actions_' . $tableId . '" data-table="' . $tableId . '">
192+
<button type="button" class="btn btn-sm btn-primary dropdown-toggle" data-toggle="dropdown">'
193+
. trans('Corals::labels.actions') .
194+
'</button>
195+
<ul class="dropdown-menu dropdown-menu-right" role="menu">' . $action_links . '</ul>
196+
</div>';
197+
}
199198
}
200199

201200
/**
@@ -611,4 +610,9 @@ public function addCheckbox(array $attributes = [], $position = false): static
611610

612611
return $this;
613612
}
613+
614+
private function isExpandableBulkActions(): bool
615+
{
616+
return $this->options['bulk_actions_as_buttons'] ?? false;
617+
}
614618
}

Corals/core/Foundation/public/assets/corals/js/corals_main.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,3 +374,35 @@ $(document).ready(function () {
374374
return date.toISOString().split('T')[0];
375375
}
376376
});
377+
378+
$(document).ready(function (){
379+
380+
let isDirty = false;
381+
382+
$('.confirmation_dirty_leave_form').on('change input', ':input', function () {
383+
isDirty = true;
384+
});
385+
386+
$('.confirmation_dirty_leave_form').on('submit', function () {
387+
isDirty = false;
388+
});
389+
390+
$('a').on('click', function (e) {
391+
if (isDirty) {
392+
e.preventDefault();
393+
themeConfirmation(
394+
corals.confirmation.title,
395+
"You have unsaved changes. Do you want to discard them and leave?",
396+
'warning',
397+
corals.confirmation.yes,
398+
corals.confirmation.cancel,
399+
function () {
400+
isDirty = false;
401+
window.location.href = $(e.target).attr('href');
402+
},
403+
function () {
404+
}
405+
);
406+
}
407+
});
408+
});

Corals/core/User/Http/Controllers/UsersController.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,11 @@ public function impersonate(Request $request, User $user)
246246
$this->authorize('impersonate', $user);
247247

248248
try {
249+
session()->put([
250+
'impersonator' => user(),
251+
'impersonate_origin_url' => url()->previous()
252+
]);
253+
249254
Auth::login($user);
250255

251256
$role = $user->roles()->first();
@@ -268,4 +273,26 @@ public function impersonate(Request $request, User $user)
268273
return response()->json($message);
269274
}
270275
}
276+
277+
public function leaveImpersonation(Request $request)
278+
{
279+
$impersonator = session()->get('impersonator');
280+
$this->authorize('leaveImpersonation', $impersonator);
281+
282+
try {
283+
Auth::login($impersonator);
284+
$redirectUrl = session()->get('impersonate_origin_url');
285+
286+
session()->forget([
287+
'impersonator',
288+
'impersonate_origin_url'
289+
]);
290+
291+
flash('Impersonation session end successfully', 'success');
292+
return redirectTo($redirectUrl);
293+
} catch (\Exception $exception) {
294+
$message = ['level' => 'error', 'message' => $exception->getMessage()];
295+
return response()->json($message);
296+
}
297+
}
271298
}

Corals/core/User/Policies/UserPolicy.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class UserPolicy extends BasePolicy
1818
'impersonate',
1919
'sendSMS',
2020
'deletedRecords',
21-
'records'
21+
'records',
22+
'leaveImpersonation',
2223
];
2324

2425
/**
@@ -161,4 +162,11 @@ public function sendSMS(User $user, UserModel $userModel): bool
161162
{
162163
return Modules::isModuleActive('corals-sms') && !is_null($userModel->getPhoneNumber());
163164
}
165+
166+
public function leaveImpersonation(User $user, ?User $impersonator = null): bool
167+
{
168+
return $impersonator && $this->impersonate($impersonator, $user);
169+
}
170+
171+
164172
}

Corals/core/User/routes/web.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
Route::get('users/{user}/address/{type}/edit', 'UserAddressesController@edit');
2020
Route::delete('users/{user}/address/{type}', 'UserAddressesController@destroy');
2121
Route::post('users/{user}/impersonate', 'UsersController@impersonate');
22+
Route::post('users/leave-impersonation', 'UsersController@leaveImpersonation')->name('impersonation.leave');
2223
Route::post('users/bulk-action', 'UsersController@bulkAction');
2324
Route::post('users/{user}/restore', 'UsersController@restore')->withTrashed();
2425
Route::delete('users/{user}/hard-delete', 'UsersController@hardDelete')->withTrashed();

public/assets/corals/js/corals_main.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,3 +374,35 @@ $(document).ready(function () {
374374
return date.toISOString().split('T')[0];
375375
}
376376
});
377+
378+
$(document).ready(function (){
379+
380+
let isDirty = false;
381+
382+
$('.confirmation_dirty_leave_form').on('change input', ':input', function () {
383+
isDirty = true;
384+
});
385+
386+
$('.confirmation_dirty_leave_form').on('submit', function () {
387+
isDirty = false;
388+
});
389+
390+
$('a').on('click', function (e) {
391+
if (isDirty) {
392+
e.preventDefault();
393+
themeConfirmation(
394+
corals.confirmation.title,
395+
"You have unsaved changes. Do you want to discard them and leave?",
396+
'warning',
397+
corals.confirmation.yes,
398+
corals.confirmation.cancel,
399+
function () {
400+
isDirty = false;
401+
window.location.href = $(e.target).attr('href');
402+
},
403+
function () {
404+
}
405+
);
406+
}
407+
});
408+
});

resources/themes/elite-admin/layouts/master.blade.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
<!DOCTYPE html>
22
<html lang="{{ \Language::getCode() }}" dir="{{ \Language::getDirection() }}">
33
<head>
4+
<style>
5+
.end-impersonation-btn {
6+
position: fixed;
7+
bottom: 10px;
8+
right: 10px;
9+
z-index: 1000;
10+
cursor: pointer;
11+
}
12+
13+
</style>
14+
415
<meta charset="utf-8">
516
<meta http-equiv="X-UA-Compatible" content="IE=edge">
617
<title>
@@ -39,6 +50,20 @@
3950
<!-- ============================================================== -->
4051
<div id="main-wrapper">
4152

53+
@can('leaveImpersonation' , [\Corals\User\Models\User::class,session()->get('impersonator')])
54+
<div class="end-impersonation-btn">
55+
<a
56+
class="btn btn-sm btn-warning text-dark"
57+
href="{{ route('impersonation.leave') }}"
58+
data-action="post"
59+
data-page_action="redirectTo"
60+
>
61+
<i class="fa fa-fw fa-power-off text-dark"></i>
62+
End Impersonation
63+
</a>
64+
</div>
65+
@endcan
66+
4267
@include('partials.header')
4368

4469
@include('partials.sidebar')

0 commit comments

Comments
 (0)