-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathUserController.php
52 lines (43 loc) · 1.55 KB
/
UserController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
namespace App\Http\Controllers;
use App\Services\GSuiteUserService;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Modules\User\Entities\User;
class UserController extends Controller
{
public function syncWithGSuite()
{
$this->authorize('syncWithGSuite', self::class);
$gsuiteUserService = new GSuiteUserService();
$gsuiteUserService->fetch(auth()->user()->email);
$this->updateEmployeeDetailsFromGSuite($gsuiteUserService->getUsers());
return redirect()->back();
}
public function syncAllWithGSuite()
{
$this->authorize('syncAllWithGSuite', self::class);
$gsuiteUserService = new GSuiteUserService();
$gsuiteUserService->fetchAll();
$this->updateEmployeeDetailsFromGSuite($gsuiteUserService->getUsers());
return redirect()->back();
}
public function updateEmployeeDetailsFromGSuite(array $gsuiteUsers)
{
foreach ($gsuiteUsers as $gsuiteUser) {
$user = User::with('employee')->findByEmail($gsuiteUser->getPrimaryEmail())->first();
if (is_null($user)) {
continue;
}
$user->employee->update([
'name' => $gsuiteUser->getName()->fullName,
'joined_on' => Carbon::parse($gsuiteUser->getCreationTime())->format(config('constants.date_format')),
'designation' => $gsuiteUser->getOrganizations()[0]['title'],
]);
}
}
public function projects()
{
return Auth::user()->activeProjects();
}
}