From: Rich Lott / Artful Robot Date: Fri, 23 Jun 2023 12:24:08 +0000 (+0100) Subject: standalone: civilint X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=f97eba3c35c99cd7a9a007052b222156e24cedf3;p=civicrm-core.git standalone: civilint --- diff --git a/ext/standaloneusers/CRM/Standaloneusers/BAO/Role.php b/ext/standaloneusers/CRM/Standaloneusers/BAO/Role.php index 58042186a8..b1072181b2 100644 --- a/ext/standaloneusers/CRM/Standaloneusers/BAO/Role.php +++ b/ext/standaloneusers/CRM/Standaloneusers/BAO/Role.php @@ -9,18 +9,18 @@ class CRM_Standaloneusers_BAO_Role extends CRM_Standaloneusers_DAO_Role { * @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; + * } */ } diff --git a/ext/standaloneusers/CRM/Standaloneusers/BAO/RolePermission.php b/ext/standaloneusers/CRM/Standaloneusers/BAO/RolePermission.php index 10a1f8eb2d..9f5c382f09 100644 --- a/ext/standaloneusers/CRM/Standaloneusers/BAO/RolePermission.php +++ b/ext/standaloneusers/CRM/Standaloneusers/BAO/RolePermission.php @@ -9,18 +9,18 @@ class CRM_Standaloneusers_BAO_RolePermission extends CRM_Standaloneusers_DAO_Rol * @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; + * } */ } diff --git a/ext/standaloneusers/CRM/Standaloneusers/BAO/User.php b/ext/standaloneusers/CRM/Standaloneusers/BAO/User.php index 715a7fcce4..e43d22205a 100644 --- a/ext/standaloneusers/CRM/Standaloneusers/BAO/User.php +++ b/ext/standaloneusers/CRM/Standaloneusers/BAO/User.php @@ -9,18 +9,18 @@ class CRM_Standaloneusers_BAO_User extends CRM_Standaloneusers_DAO_User { * @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; + * } */ } diff --git a/ext/standaloneusers/CRM/Standaloneusers/BAO/UserRole.php b/ext/standaloneusers/CRM/Standaloneusers/BAO/UserRole.php index 35ef60a0ed..27aa0fd7de 100644 --- a/ext/standaloneusers/CRM/Standaloneusers/BAO/UserRole.php +++ b/ext/standaloneusers/CRM/Standaloneusers/BAO/UserRole.php @@ -9,18 +9,18 @@ class CRM_Standaloneusers_BAO_UserRole extends CRM_Standaloneusers_DAO_UserRole * @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; + * } */ } diff --git a/ext/standaloneusers/CRM/Standaloneusers/Upgrader.php b/ext/standaloneusers/CRM/Standaloneusers/Upgrader.php index f6b9f891fe..6677403458 100644 --- a/ext/standaloneusers/CRM/Standaloneusers/Upgrader.php +++ b/ext/standaloneusers/CRM/Standaloneusers/Upgrader.php @@ -12,11 +12,11 @@ class CRM_Standaloneusers_Upgrader extends CRM_Extension_Upgrader_Base { /** * 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 @@ -33,12 +33,12 @@ class CRM_Standaloneusers_Upgrader extends CRM_Extension_Upgrader_Base { // 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 @@ -72,9 +72,9 @@ class CRM_Standaloneusers_Upgrader extends CRM_Extension_Upgrader_Base { $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); diff --git a/ext/standaloneusers/Civi/Authx/Standalone.php b/ext/standaloneusers/Civi/Authx/Standalone.php index d87296d424..0acd0562b2 100644 --- a/ext/standaloneusers/Civi/Authx/Standalone.php +++ b/ext/standaloneusers/Civi/Authx/Standalone.php @@ -37,7 +37,7 @@ class Standalone implements AuthxInterface { $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); diff --git a/ext/standaloneusers/Civi/Standalone/Security.php b/ext/standaloneusers/Civi/Standalone/Security.php index a16fdfffe0..4a503213df 100644 --- a/ext/standaloneusers/Civi/Standalone/Security.php +++ b/ext/standaloneusers/Civi/Standalone/Security.php @@ -19,7 +19,6 @@ class Security { public static $hashLength = 55; public static $hashMethod = 'sha512'; - /** * @return static */ @@ -40,6 +39,7 @@ class Security { // A normal Drupal 7 password. $hash = $this->_password_crypt(static::$hashMethod, $plaintextPassword, $storedHashedPassword); break; + default: // Invalid password return FALSE; @@ -50,7 +50,9 @@ class Security { /** * 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 @@ -85,9 +87,9 @@ class Security { */ 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; } /** @@ -97,11 +99,11 @@ class Security { */ 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; } @@ -113,11 +115,11 @@ class Security { */ 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; } @@ -149,10 +151,10 @@ class Security { $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()); @@ -174,9 +176,9 @@ class Security { */ public function updateCMSName($ufID, $email) { \Civi\Api4\User::update(FALSE) - ->addWhere('id', '=', $ufID) - ->addValue('email', $email) - ->execute(); + ->addWhere('id', '=', $ufID) + ->addValue('email', $email) + ->execute(); } /** @@ -303,7 +305,7 @@ class Security { * 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. */ @@ -339,7 +341,7 @@ class Security { } 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); @@ -361,16 +363,16 @@ class Security { * 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. @@ -382,7 +384,6 @@ class Security { return $output; } - /** * This is taken from Drupal 7.91 * @@ -393,10 +394,10 @@ class Security { * @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; @@ -422,4 +423,5 @@ class Security { return $output; } + } diff --git a/ext/standaloneusers/managed/SavedSearch_Administer_Users.mgd.php b/ext/standaloneusers/managed/SavedSearch_Administer_Users.mgd.php index d9b5341258..dfad0efbd6 100644 --- a/ext/standaloneusers/managed/SavedSearch_Administer_Users.mgd.php +++ b/ext/standaloneusers/managed/SavedSearch_Administer_Users.mgd.php @@ -108,4 +108,3 @@ ], ], ]; - diff --git a/ext/standaloneusers/tests/phpunit/Civi/Standalone/SecurityTest.php b/ext/standaloneusers/tests/phpunit/Civi/Standalone/SecurityTest.php index 1ea18cc997..76febf2141 100644 --- a/ext/standaloneusers/tests/phpunit/Civi/Standalone/SecurityTest.php +++ b/ext/standaloneusers/tests/phpunit/Civi/Standalone/SecurityTest.php @@ -6,7 +6,6 @@ use Civi\Test\CiviEnvBuilder; use Civi\Test\HeadlessInterface; use Civi\Core\HookInterface; use Civi\Test\TransactionalInterface; -use Civi\Standalone\Security; /** * FIXME - Add test description. @@ -28,6 +27,7 @@ class SecurityTest extends \PHPUnit\Framework\TestCase implements HeadlessInterf protected $originalUFPermission; protected $contactID; protected $userID; + /** * Setup used when HeadlessInterface is implemented. * @@ -41,7 +41,7 @@ class SecurityTest extends \PHPUnit\Framework\TestCase implements HeadlessInterf */ 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); } @@ -59,10 +59,10 @@ class SecurityTest extends \PHPUnit\Framework\TestCase implements HeadlessInterf 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']); @@ -76,17 +76,17 @@ class SecurityTest extends \PHPUnit\Framework\TestCase implements HeadlessInterf 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 @@ -94,8 +94,8 @@ class SecurityTest extends \PHPUnit\Framework\TestCase implements HeadlessInterf ['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) { @@ -129,10 +129,10 @@ class SecurityTest extends \PHPUnit\Framework\TestCase implements HeadlessInterf 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']; @@ -147,5 +147,4 @@ class SecurityTest extends \PHPUnit\Framework\TestCase implements HeadlessInterf return [$contactID, $userID, $security]; } - }