|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Auth; |
|
4
|
|
|
|
|
5
|
|
|
use App\Contact; |
|
6
|
|
|
use Auth; |
|
7
|
|
|
use App\User; |
|
8
|
|
|
use GrahamCampbell\GitHub\GitHubManager; |
|
9
|
|
|
use Socialite; |
|
10
|
|
|
use App\Http\Requests; |
|
11
|
|
|
use Illuminate\Http\Request; |
|
12
|
|
|
use App\Http\Controllers\Controller; |
|
13
|
|
|
|
|
14
|
|
|
class GitHubController extends Controller |
|
15
|
|
|
{ |
|
16
|
|
|
protected $github; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(GitHubManager $github) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->github = $github; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Redirect the user to the GitHub authentication page. |
|
25
|
|
|
* |
|
26
|
|
|
* @return Response |
|
27
|
|
|
*/ |
|
28
|
|
|
public function redirectToProvider() |
|
29
|
|
|
{ |
|
30
|
|
|
return Socialite::driver('github')->scopes(['read:org', 'user:email'])->redirect(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Obtain the user information from GitHub. |
|
35
|
|
|
* |
|
36
|
|
|
* @return Response |
|
37
|
|
|
*/ |
|
38
|
|
|
public function handleProviderCallback() |
|
39
|
|
|
{ |
|
40
|
|
|
$socialiteUser = Socialite::driver('github')->user(); |
|
41
|
|
|
|
|
42
|
|
|
//If the org filter is set get the users organisations and check them |
|
43
|
|
|
if (!empty(env('VALID_GITHUB_ORG'))) { |
|
44
|
|
|
$this->github->authenticate(\Github\Client::AUTH_URL_TOKEN, $socialiteUser->token); |
|
45
|
|
|
|
|
46
|
|
|
$organizations = $this->github->currentUser()->memberships()->all(); |
|
47
|
|
|
|
|
48
|
|
|
$loginValid = false; |
|
49
|
|
|
foreach ($organizations as $org) { |
|
50
|
|
|
if ($org['organization']['login'] === env('VALID_GITHUB_ORG')) { |
|
51
|
|
|
$loginValid = true; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if (!$loginValid) { |
|
56
|
|
|
return redirect('/login')->withError('Not a member of the required organisation'); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
//Locate a user or create an account |
|
61
|
|
|
$user = User::where('email', $socialiteUser->getEmail())->first(); |
|
62
|
|
|
if (!$user) { |
|
63
|
|
|
$user = User::create(['email' => $socialiteUser->getEmail(), 'name' => $socialiteUser->getName()]); |
|
64
|
|
|
//Add a contact record for the user so they receive update notifications |
|
65
|
|
|
$contact = new Contact; |
|
66
|
|
|
$contact->name = $user->name; |
|
|
|
|
|
|
67
|
|
|
$contact->email = $user->email; |
|
|
|
|
|
|
68
|
|
|
$contact->filter_tags = []; |
|
69
|
|
|
$contact->active = true; |
|
70
|
|
|
$contact->save(); |
|
71
|
|
|
} |
|
72
|
|
|
Auth::login($user, true); |
|
73
|
|
|
return redirect('/pings'); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.