Merge pull request #8494 from kcristiano/CRM-18221
[civicrm-core.git] / CRM / Core / BAO / CMSUser.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2016
32 */
33
34 /**
35 * This file contains functions for synchronizing cms users with CiviCRM contacts.
36 */
37
38 /**
39 * Class CRM_Core_BAO_CMSUser
40 */
41 class CRM_Core_BAO_CMSUser {
42
43 /**
44 * Create CMS user using Profile.
45 *
46 * @param array $params
47 * @param string $mail
48 * Email id for cms user.
49 *
50 * @return int
51 * contact id that has been created
52 */
53 public static function create(&$params, $mail) {
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
63 $ufmatch['uf_id'] = $ufID;
64 $ufmatch['contact_id'] = $params['contactID'];
65 $ufmatch['uf_name'] = $params[$mail];
66 CRM_Core_BAO_UFMatch::create($ufmatch);
67 }
68
69 return $ufID;
70 }
71
72 /**
73 * Create Form for CMS user using Profile.
74 *
75 * @param CRM_Core_Form $form
76 * @param int $gid
77 * Id of group of profile.
78 * @param bool $emailPresent
79 * True if the profile field has email(primary).
80 * @param \const|int $action
81 *
82 * @return FALSE|void
83 * WTF
84 *
85 */
86 public static function buildForm(&$form, $gid, $emailPresent, $action = CRM_Core_Action::NONE) {
87 $config = CRM_Core_Config::singleton();
88 $showCMS = FALSE;
89
90 $isDrupal = $config->userSystem->is_drupal;
91 $isJoomla = ucfirst($config->userFramework) == 'Joomla' ? TRUE : FALSE;
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
158 /**
159 * Checks that there is a valid username & email
160 * optionally checks password is present & matches DB & gets the CMS to validate
161 *
162 * @param array $fields
163 * Posted values of form.
164 * @param array $files
165 * Uploaded files if any.
166 * @param CRM_Core_Form $form
167 *
168 * @return array|bool
169 */
170 public static function formRule($fields, $files, $form) {
171 if (empty($fields['cms_create_account'])) {
172 return TRUE;
173 }
174
175 $config = CRM_Core_Config::singleton();
176
177 $isDrupal = $config->userSystem->is_drupal;
178 $isJoomla = ucfirst($config->userFramework) == 'Joomla' ? TRUE : FALSE;
179 $isWordPress = $config->userFramework == 'WordPress' ? TRUE : FALSE;
180
181 $errors = array();
182 if ($isDrupal || $isJoomla || $isWordPress) {
183 $emailName = NULL;
184 if (!empty($form->_bltID) && array_key_exists("email-{$form->_bltID}", $fields)) {
185 // this is a transaction related page
186 $emailName = 'email-' . $form->_bltID;
187 }
188 else {
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
239 }