/update/common/scripts/4.0/updatetipafriendpolicy.php

https://github.com/GunioRobot/ezpublish · PHP · 120 lines · 103 code · 10 blank · 7 comment · 6 complexity · cb7b7d01e5c60d676eb3d29400f22f33 MD5 · raw file

  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * @copyright Copyright (C) 1999-2011 eZ Systems AS. All rights reserved.
  5. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  6. * @version //autogentag//
  7. * @package kernel
  8. */
  9. require 'autoload.php';
  10. $cli = eZCLI::instance();
  11. $script = eZScript::instance( array( 'description' => "\nThis script is optional for upgrading to 3.10.\n" .
  12. "The script adds a role which contains a policy 'content/tipafriend' and" .
  13. "\nassigns this role to all user groups except anonymous. That will give " .
  14. "\npossibility to use tipafriend view for all users except anonymous." .
  15. "\n\nNote: siteacces, login and password options are required and" .
  16. "\nmust to be set to admin siteaccess and admin login/passsword accordingly" .
  17. "\n\n(See doc/feature/(3.8|3.9|3.10)/content_tipafriend_policy.txt for more information).",
  18. 'use-session' => false,
  19. 'use-modules' => false,
  20. 'use-extensions' => true ) );
  21. $script->startup();
  22. $options = $script->getOptions( '', '', false, false,
  23. array( 'siteaccess' => true,
  24. 'user' => true ) );
  25. $siteAccess = $options['siteaccess'] ? $options['siteaccess'] : false;
  26. $script->setUseSiteAccess( $siteAccess );
  27. $script->initialize();
  28. $cli->output( "\nStart." );
  29. $contentIni = eZINI::instance( 'content.ini' );
  30. $userRootNodeID = $contentIni->variable( 'NodeSettings', 'UserRootNode' );
  31. $siteIni = eZINI::instance( 'site.ini' );
  32. $anonymousUserID = $siteIni->variable( 'UserSettings', 'AnonymousUserID' );
  33. $anonymousUser = eZUser::fetch( $anonymousUserID );
  34. $anonymousUsers = array();
  35. if ( is_object( $anonymousUser ) )
  36. {
  37. $anonymousUsers = $anonymousUser->groups();
  38. $anonymousUsers[] = $anonymousUserID;
  39. }
  40. $topUserNodes = eZContentObjectTreeNode::subTreeByNodeID( array( 'Depth' => 1 ), $userRootNodeID );
  41. if ( count( $topUserNodes ) == 0 )
  42. {
  43. $cli->warning( "Unable to retrieve the user root node. Please make sure\n" .
  44. "you log in to the system with the administrator's user\n" .
  45. "acount by using the -l and -p command line options." );
  46. $script->shutdown( 1 );
  47. }
  48. $roleName = 'Tipafriend Role';
  49. $role = eZRole::fetchByName( $roleName );
  50. if ( is_object( $role ) )
  51. {
  52. $cli->warning( "The 'Tipafriend Role' already exists in the system. This means that\n" .
  53. "the script was already run before or the same role was added manually.\n" .
  54. "The role will not be added. Check the role settings of the system." );
  55. }
  56. else
  57. {
  58. $userInput = '';
  59. $usersToAssign = array();
  60. $stdin = fopen( "php://stdin", "r+" );
  61. foreach ( $topUserNodes as $userNode )
  62. {
  63. if ( $userInput != 'a' )
  64. {
  65. $name = $userNode->getName();
  66. if ( in_array( $userNode->attribute( 'contentobject_id' ), $anonymousUsers ) )
  67. $cli->output( "Note: the '$name' group/user is anonymous." );
  68. $cli->output( "Assign 'Tipafriend Role' to the '$name' group/user? y(yes)/n(no)/a(all)/s(skip all): ", false );
  69. $userInput = fgets( $stdin );
  70. $userInput = trim( $userInput );
  71. }
  72. if ( $userInput == 'y' or $userInput == 'a' )
  73. {
  74. $usersToAssign[] = $userNode->attribute( 'contentobject_id' );
  75. }
  76. else if ( $userInput == 's' )
  77. {
  78. break;
  79. }
  80. }
  81. fclose( $stdin );
  82. if ( count( $usersToAssign ) > 0 )
  83. {
  84. $role = eZRole::create( $roleName );
  85. $role->store();
  86. $role->appendPolicy( 'content', 'tipafriend' );
  87. $role->store();
  88. foreach ( $usersToAssign as $userID )
  89. {
  90. $role->assignToUser( $userID );
  91. }
  92. // clear role cache
  93. eZRole::expireCache();
  94. eZContentCacheManager::clearAllContentCache();
  95. // clear policy cache
  96. eZUser::cleanupCache();
  97. }
  98. else
  99. {
  100. $cli->notice( "\nThe role wasn't added because you didn't choose any group to assign." );
  101. }
  102. }
  103. $cli->output( "\nDone." );
  104. $script->shutdown();
  105. ?>