Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // loading abstract.php
- $loader = new Loader();
- $loader->includeAbstract();
- // admin password updater for Magento
- class Magento_Admin_Password_Updater
- {
- /**
- * Run script
- */
- public function update()
- {
- if ($this->isCommandLineInterface()) {
- $runner = new Cli_Runner();
- } else {
- $runner = new Web_Runner();
- }
- $runner->run();
- }
- /**
- * Checks if command line interface is used
- *
- * @return bool
- */
- private function isCommandLineInterface()
- {
- return (php_sapi_name() == 'cli');
- }
- }
- abstract class Runner extends Mage_Shell_Abstract
- {
- /**
- * Returns new admin user model
- *
- * @return Mage_Admin_Model_User
- */
- protected function getNewInstance()
- {
- return Mage::getModel('admin/user');
- }
- /**
- * Returns admin user by its username
- *
- * @param $username string Admin username
- *
- * @return Mage_Admin_Model_User
- */
- protected function getUser($username)
- {
- return Mage::getModel('admin/user')->loadByUsername($username);
- }
- /**
- * Returns all admin users
- *
- * @return array
- */
- protected function getUsers()
- {
- return Mage::getModel('admin/user')->getResourceCollection()->getItems();
- }
- /**
- * Save new admin user
- *
- * @param $data array Admin user data
- */
- protected function createUser($data)
- {
- $user = $this->getNewInstance();
- $user->setData($data);
- try {
- $user->save();
- } catch (Exception $e) {
- echo ':( Exception' . PHP_EOL;
- echo $e->getMessage() . PHP_EOL;
- exit;
- }
- }
- /**
- * Update user with new data
- *
- * @param Mage_Admin_Model_User $user
- * @param string $password
- */
- protected function updateUser(Mage_Admin_Model_User $user, $password)
- {
- $user->setPassword($password);
- try {
- $user->save();
- } catch (Exception $e) {
- echo ':( Exception' . PHP_EOL;
- echo $e->getMessage() . PHP_EOL;
- exit;
- }
- }
- }
- class Cli_Runner extends Runner
- {
- /**
- * Default admin user data that is used as 'me'
- *
- * @var array
- */
- private $myData = [
- 'username' => 'admin',
- 'email' => 'admin@supersite.com',
- 'password' => 'qwerty',
- ];
- /**
- * Hashed array with entered commands
- *
- * @var array
- */
- private $commands = [];
- /**
- * Additional initialization
- */
- protected function _construct()
- {
- $this->commands = array_keys($this->_args);
- }
- /**
- * Run script in command line interface mode
- */
- public function run()
- {
- switch (true) {
- case ($this->isMe()):
- $this->addMe();
- break;
- case ($this->isNewUser()):
- $this->addNewUser();
- break;
- case ($this->isSetPassword()):
- $this->setPassword();
- break;
- case ($this->isShowAllUsers()):
- $this->showAllUsers();
- break;
- default:
- echo 'No commands are matched. Help is here:' . PHP_EOL . PHP_EOL;
- echo $this->usageHelp();
- }
- }
- /**
- * Add pre-defined admin user
- */
- private function addMe()
- {
- $myData = $this->myData;
- $myData['username'] = isset($myData['username']) ? $myData['username'] : '';
- $this->processUser($myData);
- }
- /**
- * Add new admin user
- */
- private function addNewUser()
- {
- $username = $this->getThird();
- $email = $this->getFourth();
- $password = $this->getFifth();
- var_dump($this->commands);
- if (!trim($username)) {
- echo 'Username cannot be empty.' . PHP_EOL;
- exit;
- }
- if (!trim($email)) {
- echo 'Email cannot be empty.' . PHP_EOL;
- exit;
- }
- if (!trim($password)) {
- echo 'Password cannot be empty.' . PHP_EOL;
- exit;
- }
- $data = [
- 'username' => $username,
- 'email' => $email,
- 'password' => $password,
- ];
- echo "Not working; Currently email in arguments is not working ok\n";
- // $this->processUser($data);
- }
- /**
- * Update admin user with new password
- */
- private function setPassword()
- {
- $username = $this->getFifth();
- $user = $this->getUser($username);
- if (!$user->getId()) {
- echo "User {$username} not exists." . PHP_EOL;
- } else {
- $this->updateUser($user, $this->getThird());
- echo 'Updated!' . PHP_EOL;
- }
- }
- /**
- * Shows all admin users
- *
- * @return bool
- */
- private function showAllUsers()
- {
- $users = $this->getUsers();
- /**
- * @var $user Mage_Admin_Model_User
- */
- foreach ($users as $user) {
- echo $user->getData('user_id') . '| ' . $user->getData('username') . PHP_EOL;
- }
- return true;
- }
- /**
- * Process admin user
- *
- * @param $data
- */
- private function processUser($data)
- {
- $user = $this->getUser($data['username']);
- if (!$user->getId()) {
- $this->createUser($data);
- echo 'Created!' . PHP_EOL;
- } else {
- if (!$this->isOverride()) {
- echo "User {$data['username']} already exists and override is not allowed." . PHP_EOL;
- } else {
- $this->updateUser($user, $data['password']);
- echo 'Updated!' . PHP_EOL;
- }
- }
- }
- /**
- * Checks if command 'add me' was entered
- *
- * @return bool
- */
- private function isMe()
- {
- if ($this->isFirst('add') && $this->isSecond('me')) {
- return true;
- }
- return false;
- }
- /**
- * Checks if command 'add new user' was entered
- *
- * @return bool
- */
- private function isNewUser()
- {
- if ($this->isFirst('add') && $this->isSecond('user')) {
- return true;
- }
- return false;
- }
- /**
- * Checks if command 'set password' was entered
- *
- * @return bool
- */
- private function isSetPassword()
- {
- if ($this->isFirst('set') && $this->isSecond('password') && $this->isFourth('for')) {
- return true;
- }
- return false;
- }
- /**
- * Checks if command 'show all admin users' was entered
- *
- * @return bool
- */
- private function isShowAllUsers()
- {
- if ($this->isFirst('show') && $this->isSecond('all')) {
- return true;
- }
- return false;
- }
- /**
- * Checks if first entered command is the same as value
- *
- * @param $value
- *
- * @return bool
- */
- private function isFirst($value)
- {
- return ($this->getFirst() == $value);
- }
- /**
- * Checks if second entered command is the same as value
- *
- * @param $value
- *
- * @return bool
- */
- private function isSecond($value)
- {
- return ($this->getSecond() == $value);
- }
- /**
- * Checks if fourth entered command is the same as value
- *
- * @param $value
- *
- * @return bool
- */
- private function isFourth($value)
- {
- return ($this->getFourth() == $value);
- }
- /**
- * Returns first entered command
- *
- * @return string|null
- */
- private function getFirst()
- {
- return isset($this->commands[0]) ? $this->commands[0] : null;
- }
- /**
- * Returns second entered command
- *
- * @return string|null
- */
- private function getSecond()
- {
- return isset($this->commands[1]) ? $this->commands[1] : null;
- }
- /**
- * Returns third entered command
- *
- * @return string|null
- */
- private function getThird()
- {
- return isset($this->commands[2]) ? $this->commands[2] : null;
- }
- /**
- * Returns fourth entered command
- *
- * @return string|null
- */
- private function getFourth()
- {
- return isset($this->commands[3]) ? $this->commands[3] : null;
- }
- /**
- * Returns fifth entered command
- *
- * @return string|null
- */
- private function getFifth()
- {
- return isset($this->commands[4]) ? $this->commands[4] : null;
- }
- /**
- * Checks if 'override' parameter is set
- *
- * @return bool
- */
- private function isOverride()
- {
- $args = $this->_args;
- if (array_key_exists('o', $args)) {
- return true;
- }
- return false;
- }
- /**
- * Returns help usage
- *
- * @return string
- */
- public function usageHelp()
- {
- return <<<USAGE
- Usage: php -f mapu.php -- [options]
- -h Short alias for help
- -o Flag that allow override user data
- help This help
- add me [-o] Add previously specified user data
- add user [data] [-o] Add new user (username, email, password)
- set password [pswd] for [username] Set password for specified user (override is enabled)
- show all Show list of all admin users
- USAGE;
- }
- }
- class Web_Runner extends Runner
- {
- /**
- * Run script in web mode
- */
- public function run()
- {
- echo "web runner\n";
- }
- /**
- * Allow running in browser
- *
- * @return bool
- */
- protected function _validate()
- {
- return true;
- }
- }
- class Loader
- {
- public function includeAbstract()
- {
- switch (true) {
- case (file_exists('abstract.php')):
- require_once 'abstract.php';
- break;
- case (file_exists(dirname(__FILE__) . '/shell/abstract.php')):
- require_once(dirname(__FILE__) . '/shell/abstract.php');
- break;
- case (file_exists(dirname(dirname(__FILE__) . '/shell/abstract.php'))):
- require_once(dirname(dirname(__FILE__)) . '/shell/abstract.php');
- break;
- default:
- return false;
- }
- return true;
- }
- }
- // running app
- $magento = new Magento_Admin_Password_Updater();
- $magento->update();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement