Skip to content

Commit 9a38e06

Browse files
committed
Added option to create new user from the Admin Panel
1 parent ffcab1c commit 9a38e06

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

app/Http/Controllers/AdminController.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Http\Request;
66
use Illuminate\Support\Facades\Hash;
7+
use Illuminate\Auth\Events\Registered;
78

89
use Auth;
910
use Exception;
@@ -100,6 +101,36 @@ public function verifyUser(request $request)
100101
return redirect('panel/users/all');
101102
}
102103

104+
//Create new user from the Admin Panel
105+
public function createNewUser()
106+
{
107+
108+
function random_str(
109+
int $length = 64,
110+
string $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
111+
): string {
112+
if ($length < 1) {
113+
throw new \RangeException("Length must be a positive integer");
114+
}
115+
$pieces = [];
116+
$max = mb_strlen($keyspace, '8bit') - 1;
117+
for ($i = 0; $i < $length; ++$i) {
118+
$pieces []= $keyspace[random_int(0, $max)];
119+
}
120+
return implode('', $pieces);
121+
}
122+
123+
$user = User::create([
124+
'name' => 'Admin-Created-' . random_str(8),
125+
'email' => random_str(8) . '@test.com',
126+
'password' => Hash::make(random_str(32)),
127+
'role' => 'user',
128+
'block' => 'no',
129+
]);
130+
131+
return redirect('panel/edit-user/'. $user->id);
132+
}
133+
103134
//Show user to edit
104135
public function showUser(request $request)
105136
{

resources/views/panel/users.blade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@
4343
@endforeach
4444
</tbody>
4545
</table>
46+
<a href="{{ url('') }}/panel/new-user">+ Add new user</a>
4647

4748
@endsection

routes/web.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
Route::get('/panel/users/verify/-{verify}/{id}', [AdminController::class, 'verifyUser'])->name('verifyUser');
102102
Route::get('/panel/edit-user/{id}', [AdminController::class, 'showUser'])->name('showUser');
103103
Route::post('/panel/edit-user/{id}', [AdminController::class, 'editUser'])->name('editUser');
104+
Route::get('/panel/new-user', [AdminController::class, 'createNewUser'])->name('createNewUser');
104105
Route::get('/panel/pages', [AdminController::class, 'showSitePage'])->name('showSitePage');
105106
Route::post('/panel/pages', [AdminController::class, 'editSitePage'])->name('editSitePage');
106107
Route::get('/panel/advanced-config', [AdminController::class, 'showFileEditor'])->name('showFileEditor');

0 commit comments

Comments
 (0)