Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
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 | ||
94 | //if CMS is configured for not to allow creating new CMS user, | |
95 | //don't build the form,Fixed for CRM-4036 | |
96 | if ($isJoomla) { | |
97 | $userParams = JComponentHelper::getParams('com_users'); | |
98 | if (!$userParams->get('allowUserRegistration')) { | |
99 | return FALSE; | |
100 | } | |
101 | } | |
102 | elseif ($isDrupal && !variable_get('user_register', TRUE)) { | |
103 | return FALSE; | |
104 | } | |
105 | elseif ($isWordPress && !get_option('users_can_register')) { | |
106 | return FALSE; | |
107 | } | |
108 | ||
109 | if ($gid) { | |
110 | $isCMSUser = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $gid, 'is_cms_user'); | |
111 | } | |
112 | ||
113 | // $cms is true when there is email(primary location) is set in the profile field. | |
114 | $session = CRM_Core_Session::singleton(); | |
115 | $userID = $session->get('userID'); | |
116 | $showUserRegistration = FALSE; | |
117 | if ($action) { | |
118 | $showUserRegistration = TRUE; | |
119 | } | |
120 | elseif (!$action && !$userID) { | |
121 | $showUserRegistration = TRUE; | |
122 | } | |
123 | ||
124 | if ($isCMSUser && $emailPresent) { | |
125 | if ($showUserRegistration) { | |
126 | if ($isCMSUser != 2) { | |
127 | $extra = array( | |
128 | 'onclick' => "return showHideByValue('cms_create_account','','details','block','radio',false );", | |
129 | ); | |
130 | $form->addElement('checkbox', 'cms_create_account', ts('Create an account?'), NULL, $extra); | |
131 | $required = FALSE; | |
132 | } | |
133 | else { | |
134 | $form->add('hidden', 'cms_create_account', 1); | |
135 | $required = TRUE; | |
136 | } | |
137 | ||
138 | $form->assign('isCMS', $required); | |
139 | if (!$userID || $action & CRM_Core_Action::PREVIEW || $action & CRM_Core_Action::PROFILE) { | |
140 | $form->add('text', 'cms_name', ts('Username'), NULL, $required); | |
141 | if (($isDrupal && !variable_get('user_email_verification', TRUE)) OR ($isJoomla) OR ($isWordPress)) { | |
142 | $form->add('password', 'cms_pass', ts('Password')); | |
143 | $form->add('password', 'cms_confirm_pass', ts('Confirm Password')); | |
144 | } | |
145 | ||
146 | $form->addFormRule(array('CRM_Core_BAO_CMSUser', 'formRule'), $form); | |
147 | } | |
148 | $showCMS = TRUE; | |
149 | } | |
150 | } | |
151 | ||
152 | $destination = $config->userSystem->getLoginDestination($form); | |
153 | $loginURL = $config->userSystem->getLoginURL($destination); | |
154 | $form->assign('loginURL', $loginURL); | |
155 | $form->assign('showCMS', $showCMS); | |
156 | } | |
157 | ||
c490a46a | 158 | /** |
6a488035 | 159 | * Checks that there is a valid username & email |
c490a46a | 160 | * optionally checks password is present & matches DB & gets the CMS to validate |
6a488035 | 161 | * |
6a0b768e TO |
162 | * @param array $fields |
163 | * Posted values of form. | |
164 | * @param array $files | |
165 | * Uploaded files if any. | |
c490a46a | 166 | * @param CRM_Core_Form $form |
b5c2afd0 EM |
167 | * |
168 | * @return array|bool | |
169 | */ | |
00be9182 | 170 | public static function formRule($fields, $files, $form) { |
a7488080 | 171 | if (empty($fields['cms_create_account'])) { |
6a488035 TO |
172 | return TRUE; |
173 | } | |
174 | ||
175 | $config = CRM_Core_Config::singleton(); | |
176 | ||
353ffa53 TO |
177 | $isDrupal = $config->userSystem->is_drupal; |
178 | $isJoomla = ucfirst($config->userFramework) == 'Joomla' ? TRUE : FALSE; | |
6a488035 TO |
179 | $isWordPress = $config->userFramework == 'WordPress' ? TRUE : FALSE; |
180 | ||
181 | $errors = array(); | |
182 | if ($isDrupal || $isJoomla || $isWordPress) { | |
183 | $emailName = NULL; | |
c490a46a | 184 | if (!empty($form->_bltID) && array_key_exists("email-{$form->_bltID}", $fields)) { |
6a488035 | 185 | // this is a transaction related page |
c490a46a | 186 | $emailName = 'email-' . $form->_bltID; |
0db6c3e1 TO |
187 | } |
188 | else { | |
6a488035 TO |
189 | // find the email field in a profile page |
190 | foreach ($fields as $name => $dontCare) { | |
191 | if (substr($name, 0, 5) == 'email') { | |
192 | $emailName = $name; | |
193 | break; | |
194 | } | |
195 | } | |
196 | } | |
197 | ||
198 | if ($emailName == NULL) { | |
199 | $errors['_qf_default'] == ts('Could not find an email address.'); | |
200 | return $errors; | |
201 | } | |
202 | ||
203 | if (empty($fields['cms_name'])) { | |
204 | $errors['cms_name'] = ts('Please specify a username.'); | |
205 | } | |
206 | ||
207 | if (empty($fields[$emailName])) { | |
208 | $errors[$emailName] = ts('Please specify a valid email address.'); | |
209 | } | |
210 | ||
211 | if (($isDrupal && !variable_get('user_email_verification', TRUE)) OR ($isJoomla) OR ($isWordPress)) { | |
212 | if (empty($fields['cms_pass']) || | |
213 | empty($fields['cms_confirm_pass']) | |
214 | ) { | |
215 | $errors['cms_pass'] = ts('Please enter a password.'); | |
216 | } | |
217 | if ($fields['cms_pass'] != $fields['cms_confirm_pass']) { | |
218 | $errors['cms_pass'] = ts('Password and Confirm Password values are not the same.'); | |
219 | } | |
220 | } | |
221 | ||
222 | if (!empty($errors)) { | |
223 | return $errors; | |
224 | } | |
225 | ||
226 | // now check that the cms db does not have the user name and/or email | |
227 | if ($isDrupal OR $isJoomla OR $isWordPress) { | |
228 | $params = array( | |
229 | 'name' => $fields['cms_name'], | |
230 | 'mail' => $fields[$emailName], | |
231 | ); | |
232 | } | |
233 | ||
234 | $config->userSystem->checkUserNameEmailExists($params, $errors, $emailName); | |
235 | } | |
236 | return (!empty($errors)) ? $errors : TRUE; | |
237 | } | |
238 | ||
6a488035 | 239 | } |