3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
35 * This file contains functions for synchronizing cms users with CiviCRM contacts.
39 * Class CRM_Core_BAO_CMSUser
41 class CRM_Core_BAO_CMSUser
{
44 * Create CMS user using Profile.
46 * @param array $params
48 * Email id for cms user.
51 * contact id that has been created
53 public static function create(&$params, $mail) {
54 $config = CRM_Core_Config
::singleton();
56 $ufID = $config->userSystem
->createUser($params, $mail);
58 //if contact doesn't already exist create UF Match
59 if ($ufID !== FALSE &&
60 isset($params['contactID'])
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);
73 * Create Form for CMS user using Profile.
75 * @param CRM_Core_Form $form
77 * Id of group of profile.
78 * @param bool $emailPresent
79 * True if the profile field has email(primary).
80 * @param \const|int $action
86 public static function buildForm(&$form, $gid, $emailPresent, $action = CRM_Core_Action
::NONE
) {
87 $config = CRM_Core_Config
::singleton();
90 $isDrupal = $config->userSystem
->is_drupal
;
91 $isJoomla = ucfirst($config->userFramework
) == 'Joomla' ?
TRUE : FALSE;
92 $isWordPress = $config->userFramework
== 'WordPress' ?
TRUE : FALSE;
94 if (!$config->userSystem
->isUserRegistrationPermitted()) {
95 // Do not build form if CMS is not configured to allow creating users.
100 $isCMSUser = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $gid, 'is_cms_user');
103 // $cms is true when there is email(primary location) is set in the profile field.
104 $userID = CRM_Core_Session
::singleton()->get('userID');
105 $showUserRegistration = FALSE;
107 $showUserRegistration = TRUE;
109 elseif (!$action && !$userID) {
110 $showUserRegistration = TRUE;
113 if ($isCMSUser && $emailPresent) {
114 if ($showUserRegistration) {
115 if ($isCMSUser != 2) {
117 'onclick' => "return showHideByValue('cms_create_account','','details','block','radio',false );",
119 $form->addElement('checkbox', 'cms_create_account', ts('Create an account?'), NULL, $extra);
123 $form->add('hidden', 'cms_create_account', 1);
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 ($config->userSystem
->isPasswordUserGenerated()) {
131 $form->add('password', 'cms_pass', ts('Password'));
132 $form->add('password', 'cms_confirm_pass', ts('Confirm Password'));
135 $form->addFormRule(array('CRM_Core_BAO_CMSUser', 'formRule'), $form);
141 $destination = $config->userSystem
->getLoginDestination($form);
142 $loginURL = $config->userSystem
->getLoginURL($destination);
143 $form->assign('loginURL', $loginURL);
144 $form->assign('showCMS', $showCMS);
148 * Checks that there is a valid username & email
149 * optionally checks password is present & matches DB & gets the CMS to validate
151 * @param array $fields
152 * Posted values of form.
153 * @param array $files
154 * Uploaded files if any.
155 * @param CRM_Core_Form $form
159 public static function formRule($fields, $files, $form) {
160 if (empty($fields['cms_create_account'])) {
164 $config = CRM_Core_Config
::singleton();
166 $isDrupal = $config->userSystem
->is_drupal
;
167 $isJoomla = ucfirst($config->userFramework
) == 'Joomla' ?
TRUE : FALSE;
168 $isWordPress = $config->userFramework
== 'WordPress' ?
TRUE : FALSE;
171 if ($isDrupal ||
$isJoomla ||
$isWordPress) {
173 if (!empty($form->_bltID
) && array_key_exists("email-{$form->_bltID}", $fields)) {
174 // this is a transaction related page
175 $emailName = 'email-' . $form->_bltID
;
178 // find the email field in a profile page
179 foreach ($fields as $name => $dontCare) {
180 if (substr($name, 0, 5) == 'email') {
187 if ($emailName == NULL) {
188 $errors['_qf_default'] = ts('Could not find an email address.');
192 if (empty($fields['cms_name'])) {
193 $errors['cms_name'] = ts('Please specify a username.');
196 if (empty($fields[$emailName])) {
197 $errors[$emailName] = ts('Please specify a valid email address.');
200 if ($config->userSystem
->isPasswordUserGenerated()) {
201 if (empty($fields['cms_pass']) ||
202 empty($fields['cms_confirm_pass'])
204 $errors['cms_pass'] = ts('Please enter a password.');
206 if ($fields['cms_pass'] != $fields['cms_confirm_pass']) {
207 $errors['cms_pass'] = ts('Password and Confirm Password values are not the same.');
211 if (!empty($errors)) {
215 // now check that the cms db does not have the user name and/or email
216 if ($isDrupal OR $isJoomla OR $isWordPress) {
218 'name' => $fields['cms_name'],
219 'mail' => $fields[$emailName],
223 $config->userSystem
->checkUserNameEmailExists($params, $errors, $emailName);
225 return (!empty($errors)) ?
$errors : TRUE;