Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.45 KB | None | 0 0
  1. <?php
  2.  
  3. // loading abstract.php
  4. $loader = new Loader();
  5. $loader->includeAbstract();
  6.  
  7. // admin password updater for Magento
  8. class Magento_Admin_Password_Updater
  9. {
  10. /**
  11. * Run script
  12. */
  13. public function update()
  14. {
  15. if ($this->isCommandLineInterface()) {
  16. $runner = new Cli_Runner();
  17. } else {
  18. $runner = new Web_Runner();
  19. }
  20.  
  21. $runner->run();
  22. }
  23.  
  24. /**
  25. * Checks if command line interface is used
  26. *
  27. * @return bool
  28. */
  29. private function isCommandLineInterface()
  30. {
  31. return (php_sapi_name() == 'cli');
  32. }
  33. }
  34.  
  35. abstract class Runner extends Mage_Shell_Abstract
  36. {
  37. /**
  38. * Returns new admin user model
  39. *
  40. * @return Mage_Admin_Model_User
  41. */
  42. protected function getNewInstance()
  43. {
  44. return Mage::getModel('admin/user');
  45. }
  46.  
  47. /**
  48. * Returns admin user by its username
  49. *
  50. * @param $username string Admin username
  51. *
  52. * @return Mage_Admin_Model_User
  53. */
  54. protected function getUser($username)
  55. {
  56. return Mage::getModel('admin/user')->loadByUsername($username);
  57. }
  58.  
  59. /**
  60. * Returns all admin users
  61. *
  62. * @return array
  63. */
  64. protected function getUsers()
  65. {
  66. return Mage::getModel('admin/user')->getResourceCollection()->getItems();
  67. }
  68.  
  69. /**
  70. * Save new admin user
  71. *
  72. * @param $data array Admin user data
  73. */
  74. protected function createUser($data)
  75. {
  76. $user = $this->getNewInstance();
  77. $user->setData($data);
  78.  
  79. try {
  80. $user->save();
  81. } catch (Exception $e) {
  82. echo ':( Exception' . PHP_EOL;
  83. echo $e->getMessage() . PHP_EOL;
  84.  
  85. exit;
  86. }
  87. }
  88.  
  89. /**
  90. * Update user with new data
  91. *
  92. * @param Mage_Admin_Model_User $user
  93. * @param string $password
  94. */
  95. protected function updateUser(Mage_Admin_Model_User $user, $password)
  96. {
  97. $user->setPassword($password);
  98.  
  99. try {
  100. $user->save();
  101. } catch (Exception $e) {
  102. echo ':( Exception' . PHP_EOL;
  103. echo $e->getMessage() . PHP_EOL;
  104.  
  105. exit;
  106. }
  107. }
  108. }
  109.  
  110.  
  111. class Cli_Runner extends Runner
  112. {
  113. /**
  114. * Default admin user data that is used as 'me'
  115. *
  116. * @var array
  117. */
  118. private $myData = [
  119. 'username' => 'admin',
  120. 'email' => 'admin@supersite.com',
  121. 'password' => 'qwerty',
  122. ];
  123.  
  124. /**
  125. * Hashed array with entered commands
  126. *
  127. * @var array
  128. */
  129. private $commands = [];
  130.  
  131. /**
  132. * Additional initialization
  133. */
  134. protected function _construct()
  135. {
  136. $this->commands = array_keys($this->_args);
  137. }
  138.  
  139. /**
  140. * Run script in command line interface mode
  141. */
  142. public function run()
  143. {
  144. switch (true) {
  145. case ($this->isMe()):
  146. $this->addMe();
  147. break;
  148. case ($this->isNewUser()):
  149. $this->addNewUser();
  150. break;
  151. case ($this->isSetPassword()):
  152. $this->setPassword();
  153. break;
  154. case ($this->isShowAllUsers()):
  155. $this->showAllUsers();
  156. break;
  157. default:
  158. echo 'No commands are matched. Help is here:' . PHP_EOL . PHP_EOL;
  159. echo $this->usageHelp();
  160. }
  161. }
  162.  
  163. /**
  164. * Add pre-defined admin user
  165. */
  166. private function addMe()
  167. {
  168. $myData = $this->myData;
  169. $myData['username'] = isset($myData['username']) ? $myData['username'] : '';
  170.  
  171. $this->processUser($myData);
  172. }
  173.  
  174. /**
  175. * Add new admin user
  176. */
  177. private function addNewUser()
  178. {
  179. $username = $this->getThird();
  180. $email = $this->getFourth();
  181. $password = $this->getFifth();
  182.  
  183. var_dump($this->commands);
  184.  
  185. if (!trim($username)) {
  186. echo 'Username cannot be empty.' . PHP_EOL;
  187.  
  188. exit;
  189. }
  190.  
  191. if (!trim($email)) {
  192. echo 'Email cannot be empty.' . PHP_EOL;
  193.  
  194. exit;
  195. }
  196.  
  197. if (!trim($password)) {
  198. echo 'Password cannot be empty.' . PHP_EOL;
  199.  
  200. exit;
  201. }
  202.  
  203. $data = [
  204. 'username' => $username,
  205. 'email' => $email,
  206. 'password' => $password,
  207. ];
  208.  
  209. echo "Not working; Currently email in arguments is not working ok\n";
  210.  
  211. // $this->processUser($data);
  212. }
  213.  
  214. /**
  215. * Update admin user with new password
  216. */
  217. private function setPassword()
  218. {
  219. $username = $this->getFifth();
  220. $user = $this->getUser($username);
  221.  
  222. if (!$user->getId()) {
  223. echo "User {$username} not exists." . PHP_EOL;
  224. } else {
  225. $this->updateUser($user, $this->getThird());
  226. echo 'Updated!' . PHP_EOL;
  227. }
  228. }
  229.  
  230. /**
  231. * Shows all admin users
  232. *
  233. * @return bool
  234. */
  235. private function showAllUsers()
  236. {
  237. $users = $this->getUsers();
  238.  
  239. /**
  240. * @var $user Mage_Admin_Model_User
  241. */
  242. foreach ($users as $user) {
  243. echo $user->getData('user_id') . '| ' . $user->getData('username') . PHP_EOL;
  244. }
  245.  
  246. return true;
  247. }
  248.  
  249. /**
  250. * Process admin user
  251. *
  252. * @param $data
  253. */
  254. private function processUser($data)
  255. {
  256. $user = $this->getUser($data['username']);
  257.  
  258. if (!$user->getId()) {
  259. $this->createUser($data);
  260. echo 'Created!' . PHP_EOL;
  261. } else {
  262. if (!$this->isOverride()) {
  263. echo "User {$data['username']} already exists and override is not allowed." . PHP_EOL;
  264. } else {
  265. $this->updateUser($user, $data['password']);
  266. echo 'Updated!' . PHP_EOL;
  267. }
  268. }
  269. }
  270.  
  271. /**
  272. * Checks if command 'add me' was entered
  273. *
  274. * @return bool
  275. */
  276. private function isMe()
  277. {
  278. if ($this->isFirst('add') && $this->isSecond('me')) {
  279.  
  280. return true;
  281. }
  282.  
  283. return false;
  284. }
  285.  
  286. /**
  287. * Checks if command 'add new user' was entered
  288. *
  289. * @return bool
  290. */
  291. private function isNewUser()
  292. {
  293. if ($this->isFirst('add') && $this->isSecond('user')) {
  294.  
  295. return true;
  296. }
  297.  
  298. return false;
  299. }
  300.  
  301. /**
  302. * Checks if command 'set password' was entered
  303. *
  304. * @return bool
  305. */
  306. private function isSetPassword()
  307. {
  308. if ($this->isFirst('set') && $this->isSecond('password') && $this->isFourth('for')) {
  309.  
  310. return true;
  311. }
  312.  
  313. return false;
  314. }
  315.  
  316. /**
  317. * Checks if command 'show all admin users' was entered
  318. *
  319. * @return bool
  320. */
  321. private function isShowAllUsers()
  322. {
  323. if ($this->isFirst('show') && $this->isSecond('all')) {
  324.  
  325. return true;
  326. }
  327.  
  328. return false;
  329. }
  330.  
  331. /**
  332. * Checks if first entered command is the same as value
  333. *
  334. * @param $value
  335. *
  336. * @return bool
  337. */
  338. private function isFirst($value)
  339. {
  340. return ($this->getFirst() == $value);
  341. }
  342.  
  343. /**
  344. * Checks if second entered command is the same as value
  345. *
  346. * @param $value
  347. *
  348. * @return bool
  349. */
  350. private function isSecond($value)
  351. {
  352. return ($this->getSecond() == $value);
  353. }
  354.  
  355. /**
  356. * Checks if fourth entered command is the same as value
  357. *
  358. * @param $value
  359. *
  360. * @return bool
  361. */
  362. private function isFourth($value)
  363. {
  364. return ($this->getFourth() == $value);
  365. }
  366.  
  367. /**
  368. * Returns first entered command
  369. *
  370. * @return string|null
  371. */
  372. private function getFirst()
  373. {
  374. return isset($this->commands[0]) ? $this->commands[0] : null;
  375. }
  376.  
  377. /**
  378. * Returns second entered command
  379. *
  380. * @return string|null
  381. */
  382. private function getSecond()
  383. {
  384. return isset($this->commands[1]) ? $this->commands[1] : null;
  385. }
  386.  
  387.  
  388. /**
  389. * Returns third entered command
  390. *
  391. * @return string|null
  392. */
  393. private function getThird()
  394. {
  395. return isset($this->commands[2]) ? $this->commands[2] : null;
  396. }
  397.  
  398. /**
  399. * Returns fourth entered command
  400. *
  401. * @return string|null
  402. */
  403. private function getFourth()
  404. {
  405. return isset($this->commands[3]) ? $this->commands[3] : null;
  406. }
  407.  
  408. /**
  409. * Returns fifth entered command
  410. *
  411. * @return string|null
  412. */
  413. private function getFifth()
  414. {
  415. return isset($this->commands[4]) ? $this->commands[4] : null;
  416. }
  417.  
  418. /**
  419. * Checks if 'override' parameter is set
  420. *
  421. * @return bool
  422. */
  423. private function isOverride()
  424. {
  425. $args = $this->_args;
  426.  
  427. if (array_key_exists('o', $args)) {
  428.  
  429. return true;
  430. }
  431.  
  432. return false;
  433. }
  434.  
  435. /**
  436. * Returns help usage
  437. *
  438. * @return string
  439. */
  440. public function usageHelp()
  441. {
  442. return <<<USAGE
  443. Usage: php -f mapu.php -- [options]
  444. -h Short alias for help
  445. -o Flag that allow override user data
  446. help This help
  447. add me [-o] Add previously specified user data
  448. add user [data] [-o] Add new user (username, email, password)
  449. set password [pswd] for [username] Set password for specified user (override is enabled)
  450. show all Show list of all admin users
  451.  
  452. USAGE;
  453. }
  454. }
  455.  
  456. class Web_Runner extends Runner
  457. {
  458. /**
  459. * Run script in web mode
  460. */
  461. public function run()
  462. {
  463. echo "web runner\n";
  464. }
  465.  
  466. /**
  467. * Allow running in browser
  468. *
  469. * @return bool
  470. */
  471. protected function _validate()
  472. {
  473. return true;
  474. }
  475. }
  476.  
  477. class Loader
  478. {
  479. public function includeAbstract()
  480. {
  481. switch (true) {
  482. case (file_exists('abstract.php')):
  483. require_once 'abstract.php';
  484. break;
  485. case (file_exists(dirname(__FILE__) . '/shell/abstract.php')):
  486. require_once(dirname(__FILE__) . '/shell/abstract.php');
  487. break;
  488. case (file_exists(dirname(dirname(__FILE__) . '/shell/abstract.php'))):
  489. require_once(dirname(dirname(__FILE__)) . '/shell/abstract.php');
  490. break;
  491. default:
  492. return false;
  493. }
  494.  
  495. return true;
  496. }
  497. }
  498.  
  499. // running app
  500. $magento = new Magento_Admin_Password_Updater();
  501. $magento->update();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement