3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This file contains functions for synchronizing cms users with CiviCRM contacts.
23 * Class CRM_Core_BAO_CMSUser
25 class CRM_Core_BAO_CMSUser
{
28 * Create CMS user using Profile.
30 * @param array $params
32 * Email id for cms user.
35 * contact id that has been created
37 public static function create(&$params, $mail) {
38 $config = CRM_Core_Config
::singleton();
40 $ufID = $config->userSystem
->createUser($params, $mail);
42 //if contact doesn't already exist create UF Match
43 if ($ufID !== FALSE &&
44 isset($params['contactID'])
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);
57 * Create Form for CMS user using Profile.
59 * @param CRM_Core_Form $form
61 * Id of group of profile.
62 * @param bool $emailPresent
63 * True if the profile field has email(primary).
64 * @param \const|int $action
70 public static function buildForm(&$form, $gid, $emailPresent, $action = CRM_Core_Action
::NONE
) {
71 $config = CRM_Core_Config
::singleton();
74 $isDrupal = $config->userSystem
->is_drupal
;
75 $isJoomla = ucfirst($config->userFramework
) == 'Joomla' ?
TRUE : FALSE;
76 $isWordPress = $config->userFramework
== 'WordPress' ?
TRUE : FALSE;
78 if (!$config->userSystem
->isUserRegistrationPermitted()) {
79 // Do not build form if CMS is not configured to allow creating users.
84 $isCMSUser = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $gid, 'is_cms_user');
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;
91 $showUserRegistration = TRUE;
93 elseif (!$action && !$userID) {
94 $showUserRegistration = TRUE;
97 if ($isCMSUser && $emailPresent) {
98 if ($showUserRegistration) {
99 if ($isCMSUser != 2) {
101 'onclick' => "return showHideByValue('cms_create_account','','details','block','radio',false );",
103 $form->addElement('checkbox', 'cms_create_account', ts('Create an account?'), NULL, $extra);
107 $form->add('hidden', 'cms_create_account', 1);
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'));
119 $form->addFormRule(['CRM_Core_BAO_CMSUser', 'formRule'], $form);
125 $destination = $config->userSystem
->getLoginDestination($form);
126 $loginURL = $config->userSystem
->getLoginURL($destination);
127 $form->assign('loginURL', $loginURL);
128 $form->assign('showCMS', $showCMS);
132 * Checks that there is a valid username & email
133 * optionally checks password is present & matches DB & gets the CMS to validate
135 * @param array $fields
136 * Posted values of form.
137 * @param array $files
138 * Uploaded files if any.
139 * @param CRM_Core_Form $form
143 public static function formRule($fields, $files, $form) {
144 if (empty($fields['cms_create_account'])) {
148 $config = CRM_Core_Config
::singleton();
150 $isDrupal = $config->userSystem
->is_drupal
;
151 $isJoomla = ucfirst($config->userFramework
) == 'Joomla' ?
TRUE : FALSE;
152 $isWordPress = $config->userFramework
== 'WordPress' ?
TRUE : FALSE;
155 if ($isDrupal ||
$isJoomla ||
$isWordPress) {
157 if (!empty($form->_bltID
) && array_key_exists("email-{$form->_bltID}", $fields)) {
158 // this is a transaction related page
159 $emailName = 'email-' . $form->_bltID
;
162 // find the email field in a profile page
163 foreach ($fields as $name => $dontCare) {
164 if (substr($name, 0, 5) == 'email') {
171 if ($emailName == NULL) {
172 $errors['_qf_default'] = ts('Could not find an email address.');
176 if (empty($fields['cms_name'])) {
177 $errors['cms_name'] = ts('Please specify a username.');
180 if (empty($fields[$emailName])) {
181 $errors[$emailName] = ts('Please specify a valid email address.');
184 if ($config->userSystem
->isPasswordUserGenerated()) {
185 if (empty($fields['cms_pass']) ||
186 empty($fields['cms_confirm_pass'])
188 $errors['cms_pass'] = ts('Please enter a password.');
190 if ($fields['cms_pass'] != $fields['cms_confirm_pass']) {
191 $errors['cms_pass'] = ts('Password and Confirm Password values are not the same.');
195 if (!empty($errors)) {
199 // now check that the cms db does not have the user name and/or email
200 if ($isDrupal or $isJoomla or $isWordPress) {
202 'name' => $fields['cms_name'],
203 'mail' => $fields[$emailName],
207 $config->userSystem
->checkUserNameEmailExists($params, $errors, $emailName);
209 return (!empty($errors)) ?
$errors : TRUE;