3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
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 CMS is configured for not to allow creating new CMS user,
95 //don't build the form,Fixed for CRM-4036
97 $userParams = JComponentHelper
::getParams('com_users');
98 if (!$userParams->get('allowUserRegistration')) {
102 elseif ($isDrupal && !variable_get('user_register', TRUE)) {
105 elseif ($isWordPress && !get_option('users_can_register')) {
110 $isCMSUser = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $gid, 'is_cms_user');
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;
118 $showUserRegistration = TRUE;
120 elseif (!$action && !$userID) {
121 $showUserRegistration = TRUE;
124 if ($isCMSUser && $emailPresent) {
125 if ($showUserRegistration) {
126 if ($isCMSUser != 2) {
128 'onclick' => "return showHideByValue('cms_create_account','','details','block','radio',false );",
130 $form->addElement('checkbox', 'cms_create_account', ts('Create an account?'), NULL, $extra);
134 $form->add('hidden', 'cms_create_account', 1);
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'));
146 $form->addFormRule(array('CRM_Core_BAO_CMSUser', 'formRule'), $form);
152 $destination = $config->userSystem
->getLoginDestination($form);
153 $loginURL = $config->userSystem
->getLoginURL($destination);
154 $form->assign('loginURL', $loginURL);
155 $form->assign('showCMS', $showCMS);
159 * Checks that there is a valid username & email
160 * optionally checks password is present & matches DB & gets the CMS to validate
162 * @param array $fields
163 * Posted values of form.
164 * @param array $files
165 * Uploaded files if any.
166 * @param CRM_Core_Form $form
170 public static function formRule($fields, $files, $form) {
171 if (empty($fields['cms_create_account'])) {
175 $config = CRM_Core_Config
::singleton();
177 $isDrupal = $config->userSystem
->is_drupal
;
178 $isJoomla = ucfirst($config->userFramework
) == 'Joomla' ?
TRUE : FALSE;
179 $isWordPress = $config->userFramework
== 'WordPress' ?
TRUE : FALSE;
182 if ($isDrupal ||
$isJoomla ||
$isWordPress) {
184 if (!empty($form->_bltID
) && array_key_exists("email-{$form->_bltID}", $fields)) {
185 // this is a transaction related page
186 $emailName = 'email-' . $form->_bltID
;
189 // find the email field in a profile page
190 foreach ($fields as $name => $dontCare) {
191 if (substr($name, 0, 5) == 'email') {
198 if ($emailName == NULL) {
199 $errors['_qf_default'] == ts('Could not find an email address.');
203 if (empty($fields['cms_name'])) {
204 $errors['cms_name'] = ts('Please specify a username.');
207 if (empty($fields[$emailName])) {
208 $errors[$emailName] = ts('Please specify a valid email address.');
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'])
215 $errors['cms_pass'] = ts('Please enter a password.');
217 if ($fields['cms_pass'] != $fields['cms_confirm_pass']) {
218 $errors['cms_pass'] = ts('Password and Confirm Password values are not the same.');
222 if (!empty($errors)) {
226 // now check that the cms db does not have the user name and/or email
227 if ($isDrupal OR $isJoomla OR $isWordPress) {
229 'name' => $fields['cms_name'],
230 'mail' => $fields[$emailName],
234 $config->userSystem
->checkUserNameEmailExists($params, $errors, $emailName);
236 return (!empty($errors)) ?
$errors : TRUE;