Merge pull request #19095 from nishant-bhorodia/Issue#537-owner-notification-email...
[civicrm-core.git] / CRM / Core / BAO / CMSUser.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This file contains functions for synchronizing cms users with CiviCRM contacts.
20 */
21
22 /**
23 * Class CRM_Core_BAO_CMSUser
24 */
25 class CRM_Core_BAO_CMSUser {
26
27 /**
28 * Create CMS user using Profile.
29 *
30 * @param array $params
31 * @param string $mail
32 * Email id for cms user.
33 *
34 * @return int
35 * contact id that has been created
36 */
37 public static function create(&$params, $mail) {
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
47 $ufmatch['uf_id'] = $ufID;
48 $ufmatch['contact_id'] = $params['contactID'];
49 $ufmatch['uf_name'] = $params[$mail];
50 CRM_Core_BAO_UFMatch::create($ufmatch);
51 }
52
53 return $ufID;
54 }
55
56 /**
57 * Create Form for CMS user using Profile.
58 *
59 * @param CRM_Core_Form $form
60 * @param int $gid
61 * Id of group of profile.
62 * @param bool $emailPresent
63 * True if the profile field has email(primary).
64 * @param \const|int $action
65 *
66 * @return FALSE|void
67 * WTF
68 *
69 */
70 public static function buildForm(&$form, $gid, $emailPresent, $action = CRM_Core_Action::NONE) {
71 $config = CRM_Core_Config::singleton();
72 $showCMS = FALSE;
73
74 $isDrupal = $config->userSystem->is_drupal;
75 $isJoomla = ucfirst($config->userFramework) == 'Joomla';
76 $isWordPress = $config->userFramework == 'WordPress';
77
78 if (!$config->userSystem->isUserRegistrationPermitted()) {
79 // Do not build form if CMS is not configured to allow creating users.
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.
88 $userID = CRM_Core_Session::singleton()->get('userID');
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) {
100 $extra = [
101 'onclick' => "return showHideByValue('cms_create_account','','details','block','radio',false );",
102 ];
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);
114 if ($config->userSystem->isPasswordUserGenerated()) {
115 $form->add('password', 'cms_pass', ts('Password'));
116 $form->add('password', 'cms_confirm_pass', ts('Confirm Password'));
117 }
118
119 $form->addFormRule(['CRM_Core_BAO_CMSUser', 'formRule'], $form);
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
131 /**
132 * Checks that there is a valid username & email
133 * optionally checks password is present & matches DB & gets the CMS to validate
134 *
135 * @param array $fields
136 * Posted values of form.
137 * @param array $files
138 * Uploaded files if any.
139 * @param CRM_Core_Form $form
140 *
141 * @return array|bool
142 */
143 public static function formRule($fields, $files, $form) {
144 if (empty($fields['cms_create_account'])) {
145 return TRUE;
146 }
147
148 $config = CRM_Core_Config::singleton();
149
150 $isDrupal = $config->userSystem->is_drupal;
151 $isJoomla = ucfirst($config->userFramework) == 'Joomla';
152 $isWordPress = $config->userFramework == 'WordPress';
153
154 $errors = [];
155 if ($isDrupal || $isJoomla || $isWordPress) {
156 $emailName = NULL;
157 if (!empty($form->_bltID) && array_key_exists("email-{$form->_bltID}", $fields)) {
158 // this is a transaction related page
159 $emailName = 'email-' . $form->_bltID;
160 }
161 else {
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) {
172 $errors['_qf_default'] = ts('Could not find an email address.');
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
184 if ($config->userSystem->isPasswordUserGenerated()) {
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
200 if ($isDrupal or $isJoomla or $isWordPress) {
201 $params = [
202 'name' => $fields['cms_name'],
203 'mail' => $fields[$emailName],
204 ];
205 }
206
207 $config->userSystem->checkUserNameEmailExists($params, $errors, $emailName);
208 }
209 return (!empty($errors)) ? $errors : TRUE;
210 }
211
212 }