* @param array $params key-value pairs
* @return CRM_Standaloneusers_DAO_Role|NULL
*
- public static function create($params) {
- $className = 'CRM_Standaloneusers_DAO_Role';
- $entityName = 'Role';
- $hook = empty($params['id']) ? 'create' : 'edit';
-
- CRM_Utils_Hook::pre($hook, $entityName, CRM_Utils_Array::value('id', $params), $params);
- $instance = new $className();
- $instance->copyValues($params);
- $instance->save();
- CRM_Utils_Hook::post($hook, $entityName, $instance->id, $instance);
-
- return $instance;
- } */
+ * public static function create($params) {
+ * $className = 'CRM_Standaloneusers_DAO_Role';
+ * $entityName = 'Role';
+ * $hook = empty($params['id']) ? 'create' : 'edit';
+ *
+ * CRM_Utils_Hook::pre($hook, $entityName, CRM_Utils_Array::value('id', $params), $params);
+ * $instance = new $className();
+ * $instance->copyValues($params);
+ * $instance->save();
+ * CRM_Utils_Hook::post($hook, $entityName, $instance->id, $instance);
+ *
+ * return $instance;
+ * } */
}
* @param array $params key-value pairs
* @return CRM_Standaloneusers_DAO_RolePermission|NULL
*
- public static function create($params) {
- $className = 'CRM_Standaloneusers_DAO_RolePermission';
- $entityName = 'RolePermission';
- $hook = empty($params['id']) ? 'create' : 'edit';
-
- CRM_Utils_Hook::pre($hook, $entityName, CRM_Utils_Array::value('id', $params), $params);
- $instance = new $className();
- $instance->copyValues($params);
- $instance->save();
- CRM_Utils_Hook::post($hook, $entityName, $instance->id, $instance);
-
- return $instance;
- } */
+ * public static function create($params) {
+ * $className = 'CRM_Standaloneusers_DAO_RolePermission';
+ * $entityName = 'RolePermission';
+ * $hook = empty($params['id']) ? 'create' : 'edit';
+ *
+ * CRM_Utils_Hook::pre($hook, $entityName, CRM_Utils_Array::value('id', $params), $params);
+ * $instance = new $className();
+ * $instance->copyValues($params);
+ * $instance->save();
+ * CRM_Utils_Hook::post($hook, $entityName, $instance->id, $instance);
+ *
+ * return $instance;
+ * } */
}
* @param array $params key-value pairs
* @return CRM_Standaloneusers_DAO_User|NULL
*
- public static function create($params) {
- $className = 'CRM_Standaloneusers_DAO_User';
- $entityName = 'User';
- $hook = empty($params['id']) ? 'create' : 'edit';
-
- CRM_Utils_Hook::pre($hook, $entityName, CRM_Utils_Array::value('id', $params), $params);
- $instance = new $className();
- $instance->copyValues($params);
- $instance->save();
- CRM_Utils_Hook::post($hook, $entityName, $instance->id, $instance);
-
- return $instance;
- } */
+ * public static function create($params) {
+ * $className = 'CRM_Standaloneusers_DAO_User';
+ * $entityName = 'User';
+ * $hook = empty($params['id']) ? 'create' : 'edit';
+ *
+ * CRM_Utils_Hook::pre($hook, $entityName, CRM_Utils_Array::value('id', $params), $params);
+ * $instance = new $className();
+ * $instance->copyValues($params);
+ * $instance->save();
+ * CRM_Utils_Hook::post($hook, $entityName, $instance->id, $instance);
+ *
+ * return $instance;
+ * } */
}
* @param array $params key-value pairs
* @return CRM_Standaloneusers_DAO_UserRole|NULL
*
- public static function create($params) {
- $className = 'CRM_Standaloneusers_DAO_UserRole';
- $entityName = 'UserRole';
- $hook = empty($params['id']) ? 'create' : 'edit';
-
- CRM_Utils_Hook::pre($hook, $entityName, CRM_Utils_Array::value('id', $params), $params);
- $instance = new $className();
- $instance->copyValues($params);
- $instance->save();
- CRM_Utils_Hook::post($hook, $entityName, $instance->id, $instance);
-
- return $instance;
- } */
+ * public static function create($params) {
+ * $className = 'CRM_Standaloneusers_DAO_UserRole';
+ * $entityName = 'UserRole';
+ * $hook = empty($params['id']) ? 'create' : 'edit';
+ *
+ * CRM_Utils_Hook::pre($hook, $entityName, CRM_Utils_Array::value('id', $params), $params);
+ * $instance = new $className();
+ * $instance->copyValues($params);
+ * $instance->save();
+ * CRM_Utils_Hook::post($hook, $entityName, $instance->id, $instance);
+ *
+ * return $instance;
+ * } */
}
/**
* Example: Run an external SQL script when the module is installed.
*
- public function install() {
- $this->executeSqlFile('sql/myinstall.sql');
- }
-
- /**
+ * public function install() {
+ * $this->executeSqlFile('sql/myinstall.sql');
+ * }
+ *
+ * /**
* Example: Work with entities usually not available during the install step.
*
* This method can be used for any post-install tasks. For example, if a step
// Create an admin contact.
$contactID = \Civi\Api4\Contact::create(FALSE)
- ->setValues([
- 'contact_type' => 'Individual',
- 'first_name' => 'Standalone',
- 'last_name' => 'Admin',
- ])
- ->execute()->first()['id'];
+ ->setValues([
+ 'contact_type' => 'Individual',
+ 'first_name' => 'Standalone',
+ 'last_name' => 'Admin',
+ ])
+ ->execute()->first()['id'];
$dummyEmail = 'admin@localhost.localdomain';
// Create user
$records[] = ['permission' => $permission];
}
\Civi\Api4\RolePermission::save(FALSE)
- ->setDefaults(['role_id' => $roleID])
- ->setRecords($records)
- ->execute();
+ ->setDefaults(['role_id' => $roleID])
+ ->setRecords($records)
+ ->execute();
$message = "Created New admin User $userID and contact $contactID with password $password and ALL permissions.";
\Civi::log()->notice($message);
$contactID = civicrm_api3('UFMatch', 'get', [
'sequential' => 1,
'return' => ['contact_id'],
- 'uf_id' => $userId
+ 'uf_id' => $userId,
])['values'][0]['contact_id'] ?? NULL;
// Confusingly, Civi stores it's *Contact* ID as *userId* on the session.
$session->set('userId', $contactID);
public static $hashLength = 55;
public static $hashMethod = 'sha512';
-
/**
* @return static
*/
// A normal Drupal 7 password.
$hash = $this->_password_crypt(static::$hashMethod, $plaintextPassword, $storedHashedPassword);
break;
+
default:
// Invalid password
return FALSE;
/**
* CRM_Core_Permission_Standalone::check() delegates here.
*
- * @param string $str
+ * @param \CRM_Core_Permission_Standalone $permissionObject
+ *
+ * @param string $permissionName
* The permission to check.
*
* @param int $userID
*/
public function getUserIDFromUsername(string $username): ?int {
return \Civi\Api4\User::get(FALSE)
- ->addWhere('username', '=', $username)
- ->execute()
- ->single()['id'] ?? NULL;
+ ->addWhere('username', '=', $username)
+ ->execute()
+ ->single()['id'] ?? NULL;
}
/**
*/
public function loadUserByName(string $username) {
$user = \Civi\Api4\User::get(FALSE)
- ->addWhere('username', '=', $username)
- ->addWhere('is_active', '=', TRUE)
- ->execute()->first() ?? [];
+ ->addWhere('username', '=', $username)
+ ->addWhere('is_active', '=', TRUE)
+ ->execute()->first() ?? [];
if ($user) {
- return $user;
+ return $user;
}
return FALSE;
}
*/
public function loadUserByID(int $userID) {
$user = \Civi\Api4\User::get(FALSE)
- ->addWhere('id', '=', $userID)
- ->addWhere('is_active', '=', TRUE)
- ->execute()->first() ?? [];
+ ->addWhere('id', '=', $userID)
+ ->addWhere('is_active', '=', TRUE)
+ ->execute()->first() ?? [];
if ($user) {
- return $user;
+ return $user;
}
return FALSE;
}
$hashedPassword = $this->_password_crypt(static::$hashMethod, $params['cms_pass'], $this->_password_generate_salt());
$userID = \Civi\Api4\User::create(FALSE)
- ->addValue('username', $params['cms_name'])
- ->addValue('email', $mail)
- ->addValue('password', $hashedPassword)
- ->execute()->single()['id'];
+ ->addValue('username', $params['cms_name'])
+ ->addValue('email', $mail)
+ ->addValue('password', $hashedPassword)
+ ->execute()->single()['id'];
}
catch (\Exception $e) {
\Civi::log()->warning("Failed to create user '$mail': " . $e->getMessage());
*/
public function updateCMSName($ufID, $email) {
\Civi\Api4\User::update(FALSE)
- ->addWhere('id', '=', $ufID)
- ->addValue('email', $email)
- ->execute();
+ ->addWhere('id', '=', $ufID)
+ ->addValue('email', $email)
+ ->execute();
}
/**
* An existing hash or the output of _password_generate_salt(). Must be
* at least 12 characters (the settings and salt).
*
- * @return
+ * @return string|bool
* A string containing the hashed password (and salt) or FALSE on failure.
* The return string will be truncated at DRUPAL_HASH_LENGTH characters max.
*/
} while (--$count);
$len = strlen($hash);
- $output = $setting . $this->_password_base64_encode($hash, $len);
+ $output = $setting . $this->_password_base64_encode($hash, $len);
// _password_base64_encode() of a 16 byte MD5 will always be 22 characters.
// _password_base64_encode() of a 64 byte sha512 will always be 86 characters.
$expected = 12 + ceil((8 * $len) / 6);
* Integer that determines the number of iterations used in the hashing
* process. A larger value is more secure, but takes more time to complete.
*
- * @return
+ * @return string
* A 12 character string containing the iteration count and a random salt.
*/
- public function _password_generate_salt($count_log2 = NULL) {
+ public function _password_generate_salt($count_log2 = NULL): string {
// Standalone: D7 has this stored as a CMS variable setting.
// @todo use global setting that can be changed in civicrm.settings.php
// For now, we just pick a value half way between our hard-coded min and max.
if ($count_log2 === NULL) {
- $count_log2 = (int) ((static::$maxHashCount + static::$minHashCount)/2);
+ $count_log2 = (int) ((static::$maxHashCount + static::$minHashCount) / 2);
}
$output = '$S$';
// Ensure that $count_log2 is within set bounds.
return $output;
}
-
/**
* This is taken from Drupal 7.91
*
* @param $count
* The number of characters (bytes) to encode.
*
- * @return
+ * @return string
* Encoded string
*/
- public function _password_base64_encode($input, $count) {
+ public function _password_base64_encode($input, $count): string {
$output = '';
$i = 0;
$itoa64 = self::ITOA64;
return $output;
}
+
}
use Civi\Test\HeadlessInterface;
use Civi\Core\HookInterface;
use Civi\Test\TransactionalInterface;
-use Civi\Standalone\Security;
/**
* FIXME - Add test description.
protected $originalUFPermission;
protected $contactID;
protected $userID;
+
/**
* Setup used when HeadlessInterface is implemented.
*
*/
public function setUpHeadless(): CiviEnvBuilder {
return \Civi\Test::headless()
- ->install(['authx','org.civicrm.search_kit', 'org.civicrm.afform', 'standaloneusers'])
+ ->install(['authx', 'org.civicrm.search_kit', 'org.civicrm.afform', 'standaloneusers'])
// ->installMe(__DIR__) This causes failure, so we do ↑
->apply(FALSE);
}
list($contactID, $userID, $security) = $this->createFixtureContactAndUser();
$user = \Civi\Api4\User::get(FALSE)
- ->addSelect('*', 'uf_match.*')
- ->addWhere('id', '=', $userID)
- ->addJoin('UFMatch AS uf_match', 'INNER', ['uf_match.uf_id', '=', 'id'])
- ->execute()->single();
+ ->addSelect('*', 'uf_match.*')
+ ->addWhere('id', '=', $userID)
+ ->addJoin('UFMatch AS uf_match', 'INNER', ['uf_match.uf_id', '=', 'id'])
+ ->execute()->single();
$this->assertEquals('user_one', $user['username']);
$this->assertEquals('user_one@example.org', $user['email']);
list($contactID, $userID, $security) = $this->createFixtureContactAndUser();
// Create role,
$roleID = \Civi\Api4\Role::create(FALSE)
- ->setValues([ 'name' => 'staff' ]) ->execute()->first()['id'];
+ ->setValues(['name' => 'staff'])->execute()->first()['id'];
$this->assertGreaterThan(0, $roleID);
// Assign role to user
\Civi\Api4\UserRole::create(FALSE)
- ->setValues(['user_id' => $userID, 'role_id' => $roleID])->execute();
+ ->setValues(['user_id' => $userID, 'role_id' => $roleID])->execute();
// Assign some permissions to the role.
\Civi\Api4\RolePermission::save(FALSE)
- ->setDefaults(['role_id' => $roleID])
- ->setRecords([
+ ->setDefaults(['role_id' => $roleID])
+ ->setRecords([
// Master control for access to the main CiviCRM backend and API. Give to trusted roles only.
['permission' => 'access CiviCRM'],
// Perform all tasks in the Administer CiviCRM control panel and Import Contacts
['permission' => 'view all contacts'],
['permission' => 'add contacts'],
['permission' => 'edit all contacts'],
- ])
- ->execute();
+ ])
+ ->execute();
$this->switchToOurUFClasses();
foreach (['access CiviCRM', 'view all contacts', 'add contacts', 'edit all contacts'] as $allowed) {
public function createFixtureContactAndUser(): array {
$contactID = \Civi\Api4\Contact::create(FALSE)
- ->setValues([
- 'contact_type' => 'Individual',
- 'display_name' => 'Admin McDemo',
- ])->execute()->first()['id'];
+ ->setValues([
+ 'contact_type' => 'Individual',
+ 'display_name' => 'Admin McDemo',
+ ])->execute()->first()['id'];
$security = Security::singleton();
$params = ['cms_name' => 'user_one', 'cms_pass' => 'secret1', 'notify' => FALSE, 'contactID' => $contactID, 'user_one@example.org' => 'user_one@example.org'];
return [$contactID, $userID, $security];
}
-
}