Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
192d36c5 | 19 | * This file contains functions for synchronizing cms users with CiviCRM contacts. |
6a488035 TO |
20 | */ |
21 | ||
b5c2afd0 EM |
22 | /** |
23 | * Class CRM_Core_BAO_CMSUser | |
24 | */ | |
6a488035 TO |
25 | class CRM_Core_BAO_CMSUser { |
26 | ||
6a488035 | 27 | /** |
fe482240 | 28 | * Create CMS user using Profile. |
6a488035 | 29 | * |
6a0b768e | 30 | * @param array $params |
6a0b768e TO |
31 | * @param string $mail |
32 | * Email id for cms user. | |
6a488035 | 33 | * |
a6c01b45 CW |
34 | * @return int |
35 | * contact id that has been created | |
6a488035 | 36 | */ |
00be9182 | 37 | public static function create(&$params, $mail) { |
6a488035 TO |
38 | $config = CRM_Core_Config::singleton(); |
39 | ||
40 | $ufID = $config->userSystem->createUser($params, $mail); | |
41 | ||
42 | //if contact doesn't already exist create UF Match | |
43 | if ($ufID !== FALSE && | |
44 | isset($params['contactID']) | |
45 | ) { | |
46 | // create the UF Match record | |
353ffa53 | 47 | $ufmatch['uf_id'] = $ufID; |
9163ea8f | 48 | $ufmatch['contact_id'] = $params['contactID']; |
353ffa53 | 49 | $ufmatch['uf_name'] = $params[$mail]; |
9163ea8f | 50 | CRM_Core_BAO_UFMatch::create($ufmatch); |
6a488035 TO |
51 | } |
52 | ||
53 | return $ufID; | |
54 | } | |
55 | ||
56 | /** | |
fe482240 | 57 | * Create Form for CMS user using Profile. |
6a488035 | 58 | * |
c490a46a | 59 | * @param CRM_Core_Form $form |
6a0b768e TO |
60 | * @param int $gid |
61 | * Id of group of profile. | |
62 | * @param bool $emailPresent | |
63 | * True if the profile field has email(primary). | |
da3c7979 | 64 | * @param \const|int $action |
fd31fa4c | 65 | * |
72b3a70c CW |
66 | * @return FALSE|void |
67 | * WTF | |
6a488035 | 68 | * |
6a488035 | 69 | */ |
00be9182 | 70 | public static function buildForm(&$form, $gid, $emailPresent, $action = CRM_Core_Action::NONE) { |
6a488035 TO |
71 | $config = CRM_Core_Config::singleton(); |
72 | $showCMS = FALSE; | |
73 | ||
353ffa53 TO |
74 | $isDrupal = $config->userSystem->is_drupal; |
75 | $isJoomla = ucfirst($config->userFramework) == 'Joomla' ? TRUE : FALSE; | |
6a488035 TO |
76 | $isWordPress = $config->userFramework == 'WordPress' ? TRUE : FALSE; |
77 | ||
8caad0ce | 78 | if (!$config->userSystem->isUserRegistrationPermitted()) { |
79 | // Do not build form if CMS is not configured to allow creating users. | |
6a488035 TO |
80 | return FALSE; |
81 | } | |
82 | ||
83 | if ($gid) { | |
84 | $isCMSUser = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $gid, 'is_cms_user'); | |
85 | } | |
86 | ||
87 | // $cms is true when there is email(primary location) is set in the profile field. | |
8caad0ce | 88 | $userID = CRM_Core_Session::singleton()->get('userID'); |
6a488035 TO |
89 | $showUserRegistration = FALSE; |
90 | if ($action) { | |
91 | $showUserRegistration = TRUE; | |
92 | } | |
93 | elseif (!$action && !$userID) { | |
94 | $showUserRegistration = TRUE; | |
95 | } | |
96 | ||
97 | if ($isCMSUser && $emailPresent) { | |
98 | if ($showUserRegistration) { | |
99 | if ($isCMSUser != 2) { | |
be2fb01f | 100 | $extra = [ |
6a488035 | 101 | 'onclick' => "return showHideByValue('cms_create_account','','details','block','radio',false );", |
be2fb01f | 102 | ]; |
6a488035 TO |
103 | $form->addElement('checkbox', 'cms_create_account', ts('Create an account?'), NULL, $extra); |
104 | $required = FALSE; | |
105 | } | |
106 | else { | |
107 | $form->add('hidden', 'cms_create_account', 1); | |
108 | $required = TRUE; | |
109 | } | |
110 | ||
111 | $form->assign('isCMS', $required); | |
112 | if (!$userID || $action & CRM_Core_Action::PREVIEW || $action & CRM_Core_Action::PROFILE) { | |
113 | $form->add('text', 'cms_name', ts('Username'), NULL, $required); | |
63df6889 | 114 | if ($config->userSystem->isPasswordUserGenerated()) { |
6a488035 TO |
115 | $form->add('password', 'cms_pass', ts('Password')); |
116 | $form->add('password', 'cms_confirm_pass', ts('Confirm Password')); | |
117 | } | |
118 | ||
be2fb01f | 119 | $form->addFormRule(['CRM_Core_BAO_CMSUser', 'formRule'], $form); |
6a488035 TO |
120 | } |
121 | $showCMS = TRUE; | |
122 | } | |
123 | } | |
124 | ||
125 | $destination = $config->userSystem->getLoginDestination($form); | |
126 | $loginURL = $config->userSystem->getLoginURL($destination); | |
127 | $form->assign('loginURL', $loginURL); | |
128 | $form->assign('showCMS', $showCMS); | |
129 | } | |
130 | ||
c490a46a | 131 | /** |
6a488035 | 132 | * Checks that there is a valid username & email |
c490a46a | 133 | * optionally checks password is present & matches DB & gets the CMS to validate |
6a488035 | 134 | * |
6a0b768e TO |
135 | * @param array $fields |
136 | * Posted values of form. | |
137 | * @param array $files | |
138 | * Uploaded files if any. | |
c490a46a | 139 | * @param CRM_Core_Form $form |
b5c2afd0 EM |
140 | * |
141 | * @return array|bool | |
142 | */ | |
00be9182 | 143 | public static function formRule($fields, $files, $form) { |
a7488080 | 144 | if (empty($fields['cms_create_account'])) { |
6a488035 TO |
145 | return TRUE; |
146 | } | |
147 | ||
148 | $config = CRM_Core_Config::singleton(); | |
149 | ||
353ffa53 TO |
150 | $isDrupal = $config->userSystem->is_drupal; |
151 | $isJoomla = ucfirst($config->userFramework) == 'Joomla' ? TRUE : FALSE; | |
6a488035 TO |
152 | $isWordPress = $config->userFramework == 'WordPress' ? TRUE : FALSE; |
153 | ||
be2fb01f | 154 | $errors = []; |
6a488035 TO |
155 | if ($isDrupal || $isJoomla || $isWordPress) { |
156 | $emailName = NULL; | |
c490a46a | 157 | if (!empty($form->_bltID) && array_key_exists("email-{$form->_bltID}", $fields)) { |
6a488035 | 158 | // this is a transaction related page |
c490a46a | 159 | $emailName = 'email-' . $form->_bltID; |
0db6c3e1 TO |
160 | } |
161 | else { | |
6a488035 TO |
162 | // find the email field in a profile page |
163 | foreach ($fields as $name => $dontCare) { | |
164 | if (substr($name, 0, 5) == 'email') { | |
165 | $emailName = $name; | |
166 | break; | |
167 | } | |
168 | } | |
169 | } | |
170 | ||
171 | if ($emailName == NULL) { | |
f71563d6 | 172 | $errors['_qf_default'] = ts('Could not find an email address.'); |
6a488035 TO |
173 | return $errors; |
174 | } | |
175 | ||
176 | if (empty($fields['cms_name'])) { | |
177 | $errors['cms_name'] = ts('Please specify a username.'); | |
178 | } | |
179 | ||
180 | if (empty($fields[$emailName])) { | |
181 | $errors[$emailName] = ts('Please specify a valid email address.'); | |
182 | } | |
183 | ||
63df6889 | 184 | if ($config->userSystem->isPasswordUserGenerated()) { |
6a488035 TO |
185 | if (empty($fields['cms_pass']) || |
186 | empty($fields['cms_confirm_pass']) | |
187 | ) { | |
188 | $errors['cms_pass'] = ts('Please enter a password.'); | |
189 | } | |
190 | if ($fields['cms_pass'] != $fields['cms_confirm_pass']) { | |
191 | $errors['cms_pass'] = ts('Password and Confirm Password values are not the same.'); | |
192 | } | |
193 | } | |
194 | ||
195 | if (!empty($errors)) { | |
196 | return $errors; | |
197 | } | |
198 | ||
199 | // now check that the cms db does not have the user name and/or email | |
cf9ccf98 | 200 | if ($isDrupal or $isJoomla or $isWordPress) { |
be2fb01f | 201 | $params = [ |
6a488035 TO |
202 | 'name' => $fields['cms_name'], |
203 | 'mail' => $fields[$emailName], | |
be2fb01f | 204 | ]; |
6a488035 TO |
205 | } |
206 | ||
207 | $config->userSystem->checkUserNameEmailExists($params, $errors, $emailName); | |
208 | } | |
209 | return (!empty($errors)) ? $errors : TRUE; | |
210 | } | |
211 | ||
6a488035 | 212 | } |