|
1
|
|
|
<?php namespace Mpociot\Teamwork; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Support\Facades\Config; |
|
4
|
|
|
use Mpociot\Teamwork\Events\UserInvitedToTeam; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* This file is part of Teamwork |
|
8
|
|
|
* |
|
9
|
|
|
* @license MIT |
|
10
|
|
|
* @package Teamwork |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
class Teamwork |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Laravel application |
|
17
|
|
|
* |
|
18
|
|
|
* @var \Illuminate\Foundation\Application |
|
19
|
|
|
*/ |
|
20
|
|
|
public $app; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Create a new Teamwork instance. |
|
24
|
|
|
* |
|
25
|
|
|
* @param \Illuminate\Foundation\Application $app |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct( $app ) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->app = $app; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get the currently authenticated user or null. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function user() |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->app->auth->user(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Invite an email adress to a team. |
|
42
|
|
|
* Either provide a email address or an object with an email property. |
|
43
|
|
|
* |
|
44
|
|
|
* If no team is given, the current_team_id will be used instead. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string|User $user |
|
47
|
|
|
* @param null|Team $team |
|
48
|
|
|
* @param callable $success |
|
49
|
|
|
* @throws \Exception |
|
50
|
|
|
*/ |
|
51
|
|
|
public function inviteToTeam( $user, $team = null, callable $success = null ) |
|
52
|
|
|
{ |
|
53
|
|
|
if ( is_null( $team ) ) |
|
54
|
|
|
{ |
|
55
|
|
|
$team = $this->user()->current_team_id; |
|
56
|
|
|
} elseif( is_object( $team ) ) |
|
57
|
|
|
{ |
|
58
|
|
|
$team = $team->getKey(); |
|
59
|
|
|
}elseif( is_array( $team ) ) |
|
60
|
|
|
{ |
|
61
|
|
|
$team = $team["id"]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if( is_object( $user ) && isset($user->email) ) |
|
65
|
|
|
{ |
|
66
|
|
|
$email = $user->email; |
|
67
|
|
|
} elseif( is_string($user) ) { |
|
68
|
|
|
$email = $user; |
|
69
|
|
|
} else { |
|
70
|
|
|
throw new \Exception('The provided object has no "email" attribute and is not a string.'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$invite = $this->app->make(Config::get('teamwork.invite_model')); |
|
74
|
|
|
$invite->user_id = $this->user()->getKey(); |
|
75
|
|
|
$invite->team_id = $team; |
|
76
|
|
|
$invite->type = 'invite'; |
|
77
|
|
|
$invite->email = $email; |
|
78
|
|
|
$invite->accept_token = md5( uniqid( microtime() ) ); |
|
79
|
|
|
$invite->deny_token = md5( uniqid( microtime() ) ); |
|
80
|
|
|
$invite->save(); |
|
81
|
|
|
|
|
82
|
|
|
if ( !is_null( $success ) ) |
|
83
|
|
|
{ |
|
84
|
|
|
event(new UserInvitedToTeam($invite)); |
|
85
|
|
|
return tap($success, fn() => $success( $invite )); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Checks if the given email address has a pending invite for the |
|
91
|
|
|
* provided Team |
|
92
|
|
|
* @param $email |
|
93
|
|
|
* @param Team|array|integer $team |
|
94
|
|
|
* @return bool |
|
95
|
|
|
*/ |
|
96
|
|
|
public function hasPendingInvite( $email, $team ) |
|
97
|
|
|
{ |
|
98
|
|
|
if( is_object( $team ) ) |
|
99
|
|
|
{ |
|
100
|
|
|
$team = $team->getKey(); |
|
101
|
|
|
} |
|
102
|
|
|
if( is_array( $team ) ) |
|
103
|
|
|
{ |
|
104
|
|
|
$team = $team["id"]; |
|
105
|
|
|
} |
|
106
|
|
|
return $this->app->make(Config::get('teamwork.invite_model'))->where('email', "=", $email)->where('team_id', "=", $team )->first() ? true : false; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param $token |
|
111
|
|
|
* @return mixed |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getInviteFromAcceptToken( $token ) |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->app->make(Config::get('teamwork.invite_model'))->where('accept_token', '=', $token)->first(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param TeamInvite $invite |
|
120
|
|
|
*/ |
|
121
|
|
|
public function acceptInvite( TeamInvite $invite ) |
|
122
|
|
|
{ |
|
123
|
|
|
$this->user()->attachTeam( $invite->team ); |
|
124
|
|
|
$invite->delete(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param $token |
|
129
|
|
|
* @return mixed |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getInviteFromDenyToken( $token ) |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->app->make(Config::get('teamwork.invite_model'))->where('deny_token', '=', $token)->first(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param TeamInvite $invite |
|
138
|
|
|
*/ |
|
139
|
|
|
public function denyInvite( TeamInvite $invite ) |
|
140
|
|
|
{ |
|
141
|
|
|
$invite->delete(); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|