1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class UserInvitation |
5
|
|
|
* @package FSWebWorks |
6
|
|
|
* @subpackage UserInvitation |
7
|
|
|
* |
8
|
|
|
* @property string FirstName |
9
|
|
|
* @property string Email |
10
|
|
|
* @property string TempHash |
11
|
|
|
* @property string Groups |
12
|
|
|
* @property int InvitedByID |
13
|
|
|
* @property Member InvitedBy |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
class UserInvitation extends DataObject |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Used to control whether a group selection on the invitation form is required. |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
private static $force_require_group = false; |
|
|
|
|
23
|
|
|
|
24
|
|
|
private static $db = array( |
|
|
|
|
25
|
|
|
'FirstName' => 'Varchar', |
26
|
|
|
'Email' => 'Varchar(254)', |
27
|
|
|
'TempHash' => 'Varchar', |
28
|
|
|
'Groups' => 'Text' |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
private static $has_one = array( |
|
|
|
|
32
|
|
|
'InvitedBy' => 'Member' |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
private static $indexes = array( |
|
|
|
|
36
|
|
|
'Email' => true, |
37
|
|
|
'TempHash' => true |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Removes the hash field from the list. |
42
|
|
|
* @return FieldList |
|
|
|
|
43
|
|
|
*/ |
44
|
|
|
public function getCMSFields() |
45
|
|
|
{ |
46
|
|
|
$fields = parent::getCMSFields(); |
47
|
|
|
$fields->removeByName('TempHash'); |
48
|
|
|
return $fields; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function onBeforeWrite() |
52
|
|
|
{ |
53
|
|
|
if (!$this->ID) { |
54
|
|
|
$generator = new RandomGenerator(); |
|
|
|
|
55
|
|
|
$this->TempHash = $generator->randomToken('sha1'); |
56
|
|
|
$this->InvitedByID = Member::currentUserID(); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
parent::onBeforeWrite(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Sends an invitation to the desired user |
63
|
|
|
*/ |
64
|
|
|
public function sendInvitation() |
65
|
|
|
{ |
66
|
|
|
return Email::create() |
|
|
|
|
67
|
|
|
->setFrom(Email::config()->get('admin_email')) |
68
|
|
|
->setTo($this->Email) |
69
|
|
|
->setSubject( |
70
|
|
|
_t( |
|
|
|
|
71
|
|
|
'UserInvation.EMAIL_SUBJECT', |
72
|
|
|
'Invitation from {name}', |
73
|
|
|
array('name' => $this->InvitedBy()->FirstName) |
74
|
|
|
) |
75
|
|
|
)->setTemplate('UserInvitationEmail') |
76
|
|
|
->populateTemplate( |
77
|
|
|
ArrayData::create( |
|
|
|
|
78
|
|
|
array( |
79
|
|
|
'Invite' => $this, |
80
|
|
|
'SiteURL' => Director::absoluteBaseURL(), |
81
|
|
|
) |
82
|
|
|
) |
83
|
|
|
) |
84
|
|
|
->send(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Checks if a user invite was already sent, or if a user is already a member |
89
|
|
|
* @return ValidationResult |
|
|
|
|
90
|
|
|
*/ |
91
|
|
|
public function validate() |
92
|
|
|
{ |
93
|
|
|
$valid = parent::validate(); |
94
|
|
|
|
95
|
|
|
if (self::get()->filter('Email', $this->Email)->first()) { |
96
|
|
|
// UserInvitation already sent |
97
|
|
|
$valid->error(_t('UserInvitation.INVITE_ALREADY_SENT', 'This user was already sent an invite.')); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (Member::get()->filter('Email', $this->Email)->first()) { |
101
|
|
|
// Member already exists |
102
|
|
|
$valid->error(_t( |
103
|
|
|
'UserInvitation.MEMBER_ALREADY_EXISTS', |
104
|
|
|
'This person is already a member of this system.' |
105
|
|
|
)); |
106
|
|
|
} |
107
|
|
|
return $valid; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Checks if this invitation has expired |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
public function isExpired() |
115
|
|
|
{ |
116
|
|
|
$result = false; |
117
|
|
|
$days = self::config()->get('days_to_expiry'); |
118
|
|
|
$time = SS_Datetime::now()->Format('U'); |
|
|
|
|
119
|
|
|
$ago = abs($time - strtotime($this->Created)); |
120
|
|
|
$rounded = round($ago / 86400); |
121
|
|
|
if ($rounded > $days) { |
122
|
|
|
$result = true; |
123
|
|
|
} |
124
|
|
|
return $result; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function canCreate($member = null){ |
128
|
|
|
return Permission::check('ACCESS_USER_INVITATIONS'); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths