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