public function up() { // Start order picking permission $picking = new sfGuardPermission(); $picking->setName('admin_shop_order_picking'); $picking->setDescription('Administrator orders in shop'); $picking->save(); $o_group = Doctrine::getTable('sfGuardGroup')->findOneByName('admin'); $group_order = new sfGuardGroupPermission(); $group_order->setGroupId($o_group->getId()); $group_order->setPermissionId($picking->getId()); $group_order->save(); // End order picking permission // Start order dispatch permission $dispatch = new sfGuardPermission(); $dispatch->setName('admin_shop_order_dispatch'); $dispatch->setDescription('Administrator promotions in shop'); $dispatch->save(); $p_group = Doctrine::getTable('sfGuardGroup')->findOneByName('admin'); $group_promotion = new sfGuardGroupPermission(); $group_promotion->setGroupId($p_group->getId()); $group_promotion->setPermissionId($dispatch->getId()); $group_promotion->save(); // End order dispatch permission // Add new column shipping_code to shop order table $this->addColumn('rt_shop_order', 'shipping_code', 'string', '50', array()); }
public function up() { $permission = new sfGuardPermission(); $permission->name = 'decision_management'; $permission->description = 'Decision management'; $permission->save(); }
public function up() { $permission = new sfGuardPermission(); $permission->setName('show_admin_menu'); $permission->setDescription('Administration menu to be displayed'); $permission->save(); $group = Doctrine::getTable('sfGuardGroup')->findOneByName('admin'); $group_permission = new sfGuardGroupPermission(); $group_permission->setGroupId($group->getId()); $group_permission->setPermissionId($permission->getId()); $group_permission->save(); }
public function up() { $permission = new sfGuardPermission(); $permission->setName('admin_snippet'); $permission->setDescription('Administrator permission for snippets'); $permission->save(); $group = Doctrine::getTable('sfGuardGroup')->findOneByName('admin'); $group_permission = new sfGuardGroupPermission(); $group_permission->setGroupId($group->getId()); $group_permission->setPermissionId($permission->getId()); $group_permission->save(); }
public function up() { $permission = new sfGuardPermission(); $permission->setName('admin_comment'); $permission->setDescription('Administration menu to be displayed'); $permission->save(); $group = Doctrine::getTable('sfGuardGroup')->findOneByName('admin'); $group_permission = new sfGuardGroupPermission(); $group_permission->setGroupId($group->getId()); $group_permission->setPermissionId($permission->getId()); $group_permission->save(); $this->createTable('rt_comment', array('id' => array('type' => 'integer', 'length' => '8', 'autoincrement' => '1', 'primary' => '1'), 'is_active' => array('type' => 'boolean', 'length' => '25'), 'model_id' => array('type' => 'integer', 'length' => '8'), 'model' => array('type' => 'string', 'length' => '100'), 'user_id' => array('type' => 'integer', 'length' => '8'), 'comment_id' => array('type' => 'integer', 'length' => '8'), 'author_name' => array('type' => 'string', 'length' => '255'), 'author_email' => array('type' => 'string', 'length' => '255'), 'author_website' => array('type' => 'string', 'extra' => 'link', 'length' => '255'), 'content' => array('type' => 'string', 'length' => ''), 'moderator_note' => array('type' => 'string', 'length' => ''), 'created_at' => array('notnull' => '1', 'type' => 'timestamp', 'length' => '25'), 'updated_at' => array('notnull' => '1', 'type' => 'timestamp', 'length' => '25')), array('primary' => array(0 => 'id'), 'collate' => 'utf8_unicode_ci', 'charset' => 'utf8')); $this->createTable('rt_comment_report', array('id' => array('type' => 'integer', 'length' => '8', 'autoincrement' => '1', 'primary' => '1'), 'reason' => array('type' => 'clob', 'length' => ''), 'referer' => array('type' => 'string', 'length' => '255'), 'status' => array('type' => 'enum', 'values' => array(0 => 'valid', 1 => 'invalid', 2 => 'untreated'), 'default' => 'untreated', 'length' => ''), 'comment_id' => array('type' => 'integer', 'notnull' => '1', 'length' => '8'), 'created_at' => array('notnull' => '1', 'type' => 'timestamp', 'length' => '25'), 'updated_at' => array('notnull' => '1', 'type' => 'timestamp', 'length' => '25')), array('primary' => array(0 => 'id'))); }
public function up() { // Start order permission $order = new sfGuardPermission(); $order->setName('admin_shop_order'); $order->setDescription('Administrator orders in shop'); $order->save(); $o_group = Doctrine::getTable('sfGuardGroup')->findOneByName('admin'); $group_order = new sfGuardGroupPermission(); $group_order->setGroupId($o_group->getId()); $group_order->setPermissionId($order->getId()); $group_order->save(); // End order permission // Start promotion permission $promotion = new sfGuardPermission(); $promotion->setName('admin_shop_promotion'); $promotion->setDescription('Administrator promotions in shop'); $promotion->save(); $p_group = Doctrine::getTable('sfGuardGroup')->findOneByName('admin'); $group_promotion = new sfGuardGroupPermission(); $group_promotion->setGroupId($p_group->getId()); $group_promotion->setPermissionId($promotion->getId()); $group_promotion->save(); // End promotion permission // Start voucher permission $voucher = new sfGuardPermission(); $voucher->setName('admin_shop_voucher'); $voucher->setDescription('Administrator vouchers in shop'); $voucher->save(); $v_group = Doctrine::getTable('sfGuardGroup')->findOneByName('admin'); $group_voucher = new sfGuardGroupPermission(); $group_voucher->setGroupId($v_group->getId()); $group_voucher->setPermissionId($voucher->getId()); $group_voucher->save(); // End voucher permission }
/** * Adds an object to the instance pool. * * Propel keeps cached copies of objects in an instance pool when they are retrieved * from the database. In some cases -- especially when you override doSelect*() * methods in your stub classes -- you may need to explicitly add objects * to the cache in order to ensure that the same objects are always returned by doSelect*() * and retrieveByPK*() calls. * * @param sfGuardPermission $value A sfGuardPermission object. * @param string $key (optional) key to use for instance map (for performance boost if key was already calculated externally). */ public static function addInstanceToPool(sfGuardPermission $obj, $key = null) { if (Propel::isInstancePoolingEnabled()) { if ($key === null) { $key = (string) $obj->getId(); } // if key === null self::$instances[$key] = $obj; } }
$t->info(' Check the integrity of the tree.'); test_total_nodes($t, array(0 => 1, 1 => 3, 2 => 4, 3 => 0)); root_sanity_check($t, $rt); check_child_ordering($t, $rt, array(), array('Parent 1', 'Parent 2', 'Parent 3')); $t->info('3 - Test createMenu() to create a new ioMenuItem tree from the database.'); // clear out the data Doctrine_Query::create()->from('ioDoctrineMenuItem')->delete()->execute(); Doctrine_Query::create()->from('sfGuardPermission')->delete()->execute(); // create the tree and make its vars accessible extract(create_doctrine_test_tree($t)); print_test_tree($t); $t->info(' 3.1 - Adding some Permissions for testing'); $c1 = new sfGuardPermission(); $c1->name = 'c1'; $c1->save(); $c2 = new sfGuardPermission(); $c2->name = 'c2'; $c2->save(); $rt->link('Permissions', array($c1->id, $c2->id)); $rt->save(); $t->info(' 3.2 - Creating the menu object.'); $timer = new sfTimer(); $menu = $rt->createMenu(); $timer->addTime(); $t->info(sprintf('### Menu created from db in %s sec (%s nodes/min)', round($timer->getElapsedTime(), 4), floor(8 * 60 / $timer->getElapsedTime()))); $t->info(' 3.3 - Running tests on the created menu object'); $t->is(get_class($menu), 'ioMenuItem', 'The menu rt has the correct class'); $t->is(count($menu->getChildren()), 2, 'The menu rt has 2 children'); $t->is(array_keys($menu->getChildren()), array('Parent 1', 'Parent 2')); $t->is($menu->getAttributes(), array('class' => 'root'), 'The menu rt has the correct attributes array'); $t->is($menu->getCredentials(), array('c1', 'c2'), 'The menu rt has the correct credentials array');
public function executePermissions(sfWebRequest $request) { $module = 'sfGuardUser'; if (!in_array($module, array_keys(sfPlop::getSafePluginModules()))) { $this->redirect('@sf_plop_dashboard'); } if ($request->isMethod(sfRequest::POST)) { if ($request->isXmlHttpRequest()) { $this->setTemplate('ajaxPermissions'); $this->setLayout(false); } $group_id = $request->getParameter('g'); $user_id = $request->getParameter('u'); $permission_id = $request->getParameter('p'); if ($group_id) { $group_exists = sfPlopGuard::groupExists($group_id); if (!$group_exists && $request->isXmlHttpRequest()) { return sfView::ERROR; } else { if (!$group_exists) { $this->redirect('@sf_plop_dashboard_permissions'); } } } if ($user_id) { $user_exists = sfPlopGuard::userExists($user_id); if (!$user_exists && $request->isXmlHttpRequest()) { return sfView::ERROR; } else { if (!$user_exists) { $this->redirect('@sf_plop_dashboard_permissions'); } } } if (isset($group_exists) && isset($user_exists)) { $user_group = sfGuardUserGroupPeer::retrieveByPK($user_id, $group_id); if ($user_group) { $user_group->delete(); } else { $user_group = new sfGuardUsergroup(); $user_group->setUserId($user_id); $user_group->setGroupId($group_id); $user_group->save(); $this->getResponse()->setStatusCode(201); } } if ($permission_id) { if ($permission_id == 'super') { if (!sfPlopGuard::isLastSuperAdminUser($user_id)) { $user = sfGuardUserPeer::retrieveByPK($user_id); if ($user->getIsSuperAdmin()) { $user->setIsSuperAdmin(false); } else { $user->setIsSuperAdmin(true); } $user->save(); } else { $this->getResponse()->setStatusCode(202); return sfView::ERROR; } } else { if (!is_int($permission_id)) { $permission_exists = sfPlopGuard::permissionExists($permission_id); if (!$permission_exists) { $modules = sfPlop::getSafePluginModules(); if ($request->isXmlHttpRequest() && !isset($modules[$permission_id])) { return sfView::ERROR; } elseif (!isset($modules[$permission_id])) { $this->redirect('@sf_plop_dashboard_permissions'); } else { $module = $modules[$permission_id]; } $permission = new sfGuardPermission(); $permission->setName($permission_id); $permission->setDescription($module['name']); $permission->save(); $permission_id = $permission->getId(); $this->getResponse()->setStatusCode(201); } else { $permission_id = sfPlopGuard::getPermission($permission_id)->getId(); } } else { $permission_exists = sfPlopGuard::permissionExists($permission_id); if (!$permission_exists && $request->isXmlHttpRequest()) { return sfView::ERROR; } else { if (!$permission_exists) { $this->redirect('@sf_plop_dashboard_permissions'); } } } if (isset($user_exists)) { $user_permission = sfGuardUserPermissionPeer::retrieveByPK($user_id, $permission_id); if ($user_permission) { $user_permission->delete(); } else { $user_permission = new sfGuardUserPermission(); $user_permission->setUserId($user_id); $user_permission->setPermissionId($permission_id); $user_permission->save(); $this->getResponse()->setStatusCode(201); } } elseif (isset($group_exists)) { $group_permission = sfGuardGroupPermissionPeer::retrieveByPK($group_id, $permission_id); if ($group_permission) { $group_permission->delete(); } else { $group_permission = new sfGuardGroupPermission(); $group_permission->setGroupId($group_id); $group_permission->setPermissionId($permission_id); $group_permission->save(); $this->getResponse()->setStatusCode(201); } } } } if (!$request->isXmlHttpRequest()) { $this->redirect('@sf_plop_dashboard_permissions'); } } $this->groups = sfPlopGuard::getAllGroups(); $this->users = sfPlopGuard::getAllUsers(); $this->permissions = sfPlopGuard::getAllPermissions(); }
public function createProjectPermission($name, $description) { // TODO: make sure permission does not already exist - what a PITA if (sfGuardPermissionPeer::retrieveByName($this->getUuid() . '-' . $name) != null) { return true; } $permission = new sfGuardPermission(); $permission->setName($this->getUuid() . '-' . $name); $permission->setDescription($description); $permission->save(); }
/** * Exclude object from result * * @param sfGuardPermission $sfGuardPermission Object to remove from the list of results * * @return sfGuardPermissionQuery The current query, for fluid interface */ public function prune($sfGuardPermission = null) { if ($sfGuardPermission) { $this->addUsingAlias(sfGuardPermissionPeer::ID, $sfGuardPermission->getId(), Criteria::NOT_EQUAL); } return $this; }
/** * DOCUMENT ME * @param mixed $arguments * @param mixed $options */ protected function execute($arguments = array(), $options = array()) { // We need a basic context so we can notify events $context = sfContext::createInstance($this->configuration); // initialize the database connection $databaseManager = new sfDatabaseManager($this->configuration); $connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection(); $postTasks = array(); echo "\nApostrophe Database Migration Task\n \nThis task will make any necessary database schema changes to bring your \nMySQL database up to date with the current release of Apostrophe and any additional\nApostrophe plugins that you have installed. For other databases see the source code \nor run './symfony doctrine:build-sql' to obtain the SQL commands you may need.\n \nBACK UP YOUR DATABASE BEFORE YOU RUN THIS TASK. It works fine in our tests, \nbut why take chances with your data?\n\n"; if (!$options['force']) { if (!$this->askConfirmation("Are you sure you are ready to migrate your project? [y/N]", 'QUESTION_LARGE', false)) { die("Operation CANCELLED. No changes made.\n"); } } $this->migrate = new aMigrate(Doctrine_Manager::connection()->getDbh()); // If I needed to I could look for the constraint definition like this. // But since we added these in the same migration I don't have to. Keep this // comment around as sooner or later we'll probably need to check for this // kind of thing // // $createTable = $data[0]['Create Table']; // if (!preg_match('/CONSTRAINT `a_redirect_page_id_a_page_id`/', $createTable)) // { // // } if (!$this->migrate->tableExists('a_redirect')) { $this->migrate->sql(array("CREATE TABLE IF NOT EXISTS a_redirect (id INT AUTO_INCREMENT, page_id INT, slug VARCHAR(255) UNIQUE, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, INDEX slugindex_idx (slug), INDEX page_id_idx (page_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = INNODB;", "ALTER TABLE a_redirect ADD CONSTRAINT a_redirect_page_id_a_page_id FOREIGN KEY (page_id) REFERENCES a_page(id) ON DELETE CASCADE;")); } if (!$this->migrate->columnExists('a_media_item', 'lucene_dirty')) { $this->migrate->sql(array("ALTER TABLE a_media_item ADD COLUMN lucene_dirty BOOLEAN DEFAULT false;")); } if (!$this->migrate->getCommandsRun()) { echo "Your database is already up to date.\n\n"; } else { echo $this->migrate->getCommandsRun() . " SQL commands were run.\n\n"; } if (!$this->migrate->tableExists('a_group_access')) { // They don't have a group access table yet. In theory, they don't have an editor permission // to grant to groups yet either. However that is a likely name for them to invent on their // own, so make sure we don't panic if there is already a permission called eidtor $this->migrate->sql(array('CREATE TABLE a_group_access (id BIGINT AUTO_INCREMENT, page_id INT, privilege VARCHAR(100), group_id INT, INDEX pageindex_idx (page_id), INDEX group_id_idx (group_id), PRIMARY KEY(id)) ENGINE = INNODB;', 'ALTER TABLE a_group_access ADD CONSTRAINT a_group_access_page_id_a_page_id FOREIGN KEY (page_id) REFERENCES a_page(id) ON DELETE CASCADE;', 'ALTER TABLE a_group_access ADD CONSTRAINT a_group_access_group_id_sf_guard_group_id FOREIGN KEY (group_id) REFERENCES sf_guard_group(id) ON DELETE CASCADE;', 'INSERT INTO sf_guard_permission (name, description) VALUES ("editor", "For groups that will be granted editing privileges at some point in the site") ON DUPLICATE KEY UPDATE id = id;')); } $viewLocked = sfConfig::get('app_a_view_locked_sufficient_credentials', 'view_locked'); // If they haven't customized it make sure it exists. Some pkContextCMS sites might not have it if ($viewLocked === 'view_locked') { $permission = Doctrine::getTable('sfGuardPermission')->findOneByName($viewLocked); if (!$permission) { $permission = new sfGuardPermission(); $permission->setName('view_locked'); $permission->save(); $groups = array('editor', 'admin'); foreach ($groups as $group) { $g = Doctrine::getTable('sfGuardGroup')->findOneByName($group); if ($g) { $pg = new sfGuardGroupPermission(); $pg->setGroupId($g->id); $pg->setPermissionId($permission->id); $pg->save(); } } } } if (!$this->migrate->tableExists('a_category') || !$this->migrate->tableExists('a_category_group')) { $this->migrate->sql(array("CREATE TABLE IF NOT EXISTS a_category (id INT AUTO_INCREMENT, name VARCHAR(255) UNIQUE, description TEXT, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, slug VARCHAR(255), UNIQUE INDEX a_category_sluggable_idx (slug), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = INNODB;", "CREATE TABLE IF NOT EXISTS a_category_group (category_id INT, group_id INT, PRIMARY KEY(category_id, group_id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = INNODB;", "CREATE TABLE IF NOT EXISTS a_category_user (category_id INT, user_id INT, PRIMARY KEY(category_id, user_id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = INNODB;", "CREATE TABLE `a_page_to_category` (\n `page_id` INT NOT NULL DEFAULT '0',\n `category_id` INT NOT NULL DEFAULT '0',\n PRIMARY KEY (`page_id`,`category_id`),\n KEY `a_page_to_category_category_id_a_category_id` (`category_id`),\n CONSTRAINT `a_page_to_category_category_id_a_category_id` FOREIGN KEY (`category_id`) REFERENCES `a_category` (`id`) ON DELETE CASCADE,\n CONSTRAINT `a_page_to_category_page_id_a_page_id` FOREIGN KEY (`page_id`) REFERENCES `a_page` (`id`) ON DELETE CASCADE\n ) ENGINE=InnoDB DEFAULT CHARSET=utf8", "CREATE TABLE IF NOT EXISTS `a_media_item_to_category` (\n `media_item_id` INT NOT NULL DEFAULT '0',\n `category_id` INT NOT NULL DEFAULT '0',\n PRIMARY KEY (`media_item_id`,`category_id`),\n KEY `a_media_item_to_category_category_id_a_category_id` (`category_id`)\n ) ENGINE=InnoDB DEFAULT CHARSET=utf8")); // These constraints might already be present, be tolerant $constraints = array("ALTER TABLE a_media_item_to_category ADD CONSTRAINT `a_media_item_to_category_category_id_a_category_id` FOREIGN KEY (`category_id`) REFERENCES `a_category` (`id`) ON DELETE CASCADE", "ALTER TABLE a_media_item_to_category ADD CONSTRAINT `a_media_item_to_category_media_item_id_a_media_item_id` FOREIGN KEY (`media_item_id`) REFERENCES `a_media_item` (`id`) ON DELETE CASCADE", "ALTER TABLE a_category_group ADD CONSTRAINT a_category_group_group_id_sf_guard_group_id FOREIGN KEY (group_id) REFERENCES sf_guard_group(id) ON DELETE CASCADE;", "ALTER TABLE a_category_group ADD CONSTRAINT a_category_group_category_id_a_category_id FOREIGN KEY (category_id) REFERENCES a_category(id) ON DELETE CASCADE;", "ALTER TABLE a_category_user ADD CONSTRAINT a_category_user_user_id_sf_guard_user_id FOREIGN KEY (user_id) REFERENCES sf_guard_user(id) ON DELETE CASCADE;", "ALTER TABLE a_category_user ADD CONSTRAINT a_category_user_category_id_a_category_id FOREIGN KEY (category_id) REFERENCES a_category(id) ON DELETE CASCADE;"); foreach ($constraints as $c) { try { $this->migrate->sql(array($c)); } catch (Exception $e) { echo "Error creating constraint, most likely already exists, which is OK {$c}\n"; } } if ($this->migrate->tableExists('a_media_category')) { $oldCategories = $this->migrate->query('SELECT * FROM a_media_category'); } else { $oldCategories = array(); } $newCategories = $this->migrate->query('SELECT * FROM a_category'); $nc = array(); foreach ($newCategories as $newCategory) { $nc[$newCategory['slug']] = $newCategory; } $oldIdToNewId = array(); echo "Migrating media categories to Apostrophe categories...\n"; foreach ($oldCategories as $category) { if (isset($nc[$category['slug']])) { $oldIdToNewId[$category['id']] = $nc[$category['slug']]['id']; } else { $this->migrate->query('INSERT INTO a_category (name, description, slug) VALUES (:name, :description, :slug)', $category); $oldIdToNewId[$category['id']] = $this->migrate->lastInsertId(); } } echo "Migrating from aMediaItemCategory to aMediaItemToCategory...\n"; $oldMappings = $this->migrate->query('SELECT * FROM a_media_item_category'); foreach ($oldMappings as $info) { $info['category_id'] = $oldIdToNewId[$info['media_category_id']]; $this->migrate->query('INSERT INTO a_media_item_to_category (media_item_id, category_id) VALUES (:media_item_id, :category_id)', $info); } } if (!$this->migrate->tableExists('a_embed_media_account')) { $this->migrate->sql(array('CREATE TABLE a_embed_media_account (id INT AUTO_INCREMENT, service VARCHAR(100) NOT NULL, username VARCHAR(100) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = INNODB;')); } if (!$this->migrate->columnExists('a_page', 'edit_admin_lock')) { $this->migrate->sql(array('ALTER TABLE a_page ADD COLUMN edit_admin_lock TINYINT(1) DEFAULT "0"', 'ALTER TABLE a_page ADD COLUMN view_admin_lock TINYINT(1) DEFAULT "0"')); $options = array('application' => $options['application'], 'env' => $options['env'], 'connection' => $options['connection']); $postTasks[] = array('task' => new apostropheCascadeEditPermissionsTask($this->dispatcher, $this->formatter), 'arguments' => array(), 'options' => $options); } if (!$this->migrate->columnExists('a_page', 'view_guest')) { $this->migrate->sql(array('ALTER TABLE a_page ADD COLUMN view_guest TINYINT(1) DEFAULT "1"')); $options = array('application' => $options['application'], 'env' => $options['env'], 'connection' => $options['connection']); $postTasks[] = array('task' => new apostropheCascadeEditPermissionsTask($this->dispatcher, $this->formatter), 'arguments' => array(), 'options' => $options); } // Migrate all IDs to BIGINT (the default in Doctrine 1.2) for compatibility with the // new version of sfDoctrineGuardPlugin. NOTE: we continue to use INT in create table // statements BEFORE this point because we need to set up relations with what they already // have - this call will clean that up $this->migrate->upgradeIds(); // Upgrade all charsets to UTF-8 otherwise we can't store a lot of what comes back from embed services $this->migrate->upgradeCharsets(); // We can add these constraints now that we have IDs of the right size if (!$this->migrate->constraintExists('a_media_item_to_category', 'a_media_item_to_category_category_id_a_category_id')) { $this->migrate->sql(array('ALTER TABLE a_media_item_to_category MODIFY COLUMN category_id BIGINT', 'ALTER TABLE a_media_item_to_category MODIFY COLUMN media_item_id BIGINT', "ALTER TABLE a_media_item_to_category ADD CONSTRAINT `a_media_item_to_category_category_id_a_category_id` FOREIGN KEY (`category_id`) REFERENCES `a_category` (`id`) ON DELETE CASCADE", "ALTER TABLE a_media_item_to_category ADD CONSTRAINT `a_media_item_to_category_media_item_id_a_media_item_id` FOREIGN KEY (`media_item_id`) REFERENCES `a_media_item` (`id`) ON DELETE CASCADE")); } // sfDoctrineGuardPlugin 5.0.x requires this if (!$this->migrate->columnExists('sf_guard_user', 'email_address')) { $this->migrate->sql(array('ALTER TABLE sf_guard_user ADD COLUMN first_name varchar(255) DEFAULT NULL', 'ALTER TABLE sf_guard_user ADD COLUMN last_name varchar(255) DEFAULT NULL', 'ALTER TABLE sf_guard_user ADD COLUMN email_address varchar(255) DEFAULT \'\'')); // Email addresses are mandatory and can't be null. We can't start guessing whether // you have them in some other table or not. So the best we can do is stub in // the username for uniqueness for now $this->migrate->sql(array('UPDATE sf_guard_user SET email_address = concat(username, \'@notavalidaddress\')', 'ALTER TABLE sf_guard_user ADD UNIQUE KEY `email_address` (`email_address`);')); } if (!$this->migrate->tableExists('sf_guard_forgot_password')) { $this->migrate->sql(array(' CREATE TABLE `sf_guard_forgot_password` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_id` bigint(20) NOT NULL, `unique_key` varchar(255) DEFAULT NULL, `expires_at` datetime NOT NULL, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, PRIMARY KEY (`id`), KEY `user_id_idx` (`user_id`), CONSTRAINT `sf_guard_forgot_password_user_id_sf_guard_user_id` FOREIGN KEY (`user_id`) REFERENCES `sf_guard_user` (`id`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8')); } if (!$this->migrate->columnExists('a_page', 'published_at')) { $this->migrate->sql(array('ALTER TABLE a_page ADD COLUMN published_at DATETIME DEFAULT NULL', 'UPDATE a_page SET published_at = created_at WHERE published_at IS NULL')); } // Remove any orphaned media items created by insufficiently carefully written embed services, // these can break the media repository $this->migrate->sql(array('DELETE FROM a_media_item WHERE type="video" AND embed IS NULL AND service_url IS NULL')); // Rename any tags with slashes in them to avoid breaking routes in Symfony $this->migrate->sql(array('UPDATE tag SET name = replace(name, "/", "-")')); // A chance to make plugin- and project-specific additions to the schema before Doctrine queries fail to see them sfContext::getInstance()->getEventDispatcher()->notify(new sfEvent($this->migrate, "a.migrateSchemaAdditions")); $mediaEnginePage = Doctrine::getTable('aPage')->createQuery('p')->where('p.admin IS TRUE AND p.engine = "aMedia"')->fetchOne(); if (!$mediaEnginePage) { $mediaEnginePage = new aPage(); $root = aPageTable::retrieveBySlug('/'); $mediaEnginePage->getNode()->insertAsFirstChildOf($root); } $mediaEnginePage->slug = '/admin/media'; $mediaEnginePage->engine = 'aMedia'; $mediaEnginePage->setAdmin(true); $mediaEnginePage->setPublishedAt(aDate::mysql()); $new = $mediaEnginePage->isNew(); $mediaEnginePage->save(); if ($new) { $mediaEnginePage->setTitle('Media'); } echo "Ensured there is an admin media engine\n"; echo "Finished updating tables.\n"; if (count($postTasks)) { echo "Invoking post-migration tasks...\n"; foreach ($postTasks as $taskInfo) { $taskInfo['task']->run($taskInfo['arguments'], $taskInfo['options']); } } echo "Done!\n"; }
/** * Filter the query by a related sfGuardPermission object * * @param sfGuardPermission|PropelCollection $sfGuardPermission The related object(s) to use as filter * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL * * @return sfGuardUserPermissionQuery The current query, for fluid interface */ public function filterBysfGuardPermission($sfGuardPermission, $comparison = null) { if ($sfGuardPermission instanceof sfGuardPermission) { return $this->addUsingAlias(sfGuardUserPermissionPeer::PERMISSION_ID, $sfGuardPermission->getId(), $comparison); } elseif ($sfGuardPermission instanceof PropelCollection) { if (null === $comparison) { $comparison = Criteria::IN; } return $this->addUsingAlias(sfGuardUserPermissionPeer::PERMISSION_ID, $sfGuardPermission->toKeyValue('PrimaryKey', 'Id'), $comparison); } else { throw new PropelException('filterBysfGuardPermission() only accepts arguments of type sfGuardPermission or PropelCollection'); } }
try { $activeUser->addGroupByName('test-group'); $t->pass('->addGroupByName() does not throw an exception if group exist'); } catch (Exception $e) { $t->diag($e->getMessage()); $t->fail('->addGroupByName() does not throw an exception if group exist'); } $t->is($activeUser->getGroupNames(), array('test-group'), '->getGroupNames() return array with group names'); $t->is($activeUser->hasGroup('test-group'), true, '->hasGroup() return true if user has this group'); // permission managment $t->diag('permission managment'); $t->is($activeUser->getPermissionNames(), array(), '->getPermissionNames() return empty array if no permission is set'); try { $activeUser->addPermissionByName('test-permission'); $t->fail('->addPermissionByName() does throw an exception if group not exist'); } catch (Exception $e) { $t->pass('->addPermissionByName() does throw an exception if group not exist'); } $permission = new sfGuardPermission(); $permission->name = 'test-permission'; $permission->save(); $t->is($activeUser->hasPermission('test-permission'), false, '->hasPermission() return false if user hasn\'t this group'); try { $activeUser->addPermissionByName('test-permission'); $t->pass('->addPermissionByName() does not throw an exception if permission exist'); } catch (Exception $e) { $t->diag($e->getMessage()); $t->fail('->addPermissionByName() does not throw an exception if permission exist'); } $t->is($activeUser->getPermissionNames(), array('test-permission'), '->getPermissionNames() return array with permission names'); $t->is($activeUser->hasPermission('test-permission'), true, '->hasPermission() return true if user has this group');
/** * Declares an association between this object and a sfGuardPermission object. * * @param sfGuardPermission $v * @return sfGuardUserPermission The current object (for fluent API support) * @throws PropelException */ public function setsfGuardPermission(sfGuardPermission $v = null) { if ($v === null) { $this->setPermissionId(NULL); } else { $this->setPermissionId($v->getId()); } $this->asfGuardPermission = $v; // Add binding for other direction of this n:n relationship. // If this object has already been added to the sfGuardPermission object, it will not be re-added. if ($v !== null) { $v->addsfGuardUserPermission($this); } return $this; }
public static function migrate() { $migrate = new aMigrate(Doctrine_Manager::connection()->getDbh()); $blogIsNew = false; echo "Migrating apostropheBlogPlugin...\n"; if (!$migrate->tableExists('a_blog_item')) { $migrate->sql(array(" CREATE TABLE a_blog_editor (blog_item_id BIGINT, user_id BIGINT, PRIMARY KEY(blog_item_id, user_id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = INNODB;", "CREATE TABLE a_blog_item (id BIGINT AUTO_INCREMENT, author_id BIGINT, page_id BIGINT, title VARCHAR(255) NOT NULL, slug_saved TINYINT(1) DEFAULT '0', excerpt TEXT, status VARCHAR(255) DEFAULT 'draft' NOT NULL, allow_comments TINYINT(1) DEFAULT '0' NOT NULL, template VARCHAR(255) DEFAULT 'singleColumnTemplate', published_at DATETIME, type VARCHAR(255), start_date DATE, start_time TIME, end_date DATE, end_time TIME, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, slug VARCHAR(255), INDEX a_blog_item_type_idx (type), UNIQUE INDEX a_blog_item_sluggable_idx (slug), INDEX author_id_idx (author_id), INDEX page_id_idx (page_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = INNODB;", " ALTER TABLE a_blog_editor ADD CONSTRAINT a_blog_editor_user_id_sf_guard_user_id FOREIGN KEY (user_id) REFERENCES sf_guard_user(id);", " ALTER TABLE a_blog_editor ADD CONSTRAINT a_blog_editor_blog_item_id_a_blog_item_id FOREIGN KEY (blog_item_id) REFERENCES a_blog_item(id);", " ALTER TABLE a_blog_item ADD CONSTRAINT a_blog_item_page_id_a_page_id FOREIGN KEY (page_id) REFERENCES a_page(id) ON DELETE CASCADE;", " ALTER TABLE a_blog_item ADD CONSTRAINT a_blog_item_author_id_sf_guard_user_id FOREIGN KEY (author_id) REFERENCES sf_guard_user(id) ON DELETE SET NULL;")); } if (!$migrate->columnExists('a_blog_item', 'location')) { $migrate->sql(array('ALTER TABLE a_blog_item ADD COLUMN location varchar(300)')); } if (!$migrate->columnExists('a_blog_item', 'start_time')) { $migrate->sql(array('ALTER TABLE a_blog_item ADD COLUMN start_time TIME', 'ALTER TABLE a_blog_item ADD COLUMN end_time TIME')); } if (!$migrate->tableExists('a_blog_item_to_category')) { $migrate->sql(array("CREATE TABLE a_blog_item_to_category (blog_item_id BIGINT, category_id BIGINT, PRIMARY KEY(blog_item_id, category_id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = INNODB;", "ALTER TABLE a_blog_item_to_category ADD CONSTRAINT a_blog_item_to_category_category_id_a_category_id FOREIGN KEY (category_id) REFERENCES a_category(id) ON DELETE CASCADE;", "ALTER TABLE a_blog_item_to_category ADD CONSTRAINT a_blog_item_to_category_blog_item_id_a_blog_item_id FOREIGN KEY (blog_item_id) REFERENCES a_blog_item(id) ON DELETE CASCADE;")); echo "Migrating blog categories to Apostrophe categories...\n"; $oldCategories = array(); if ($migrate->tableExists('a_blog_category')) { $oldCategories = $migrate->query('SELECT * FROM a_blog_category'); } $newCategories = $migrate->query('SELECT * FROM a_category'); $nc = array(); foreach ($newCategories as $newCategory) { $nc[$newCategory['name']] = $newCategory; } $oldIdToNewId = array(); foreach ($oldCategories as $category) { if (isset($nc[$category['name']])) { $oldIdToNewId[$category['id']] = $nc[$category['name']]['id']; } else { // Blog categories didn't have slugs $category['slug'] = aTools::slugify($category['name']); $migrate->query('INSERT INTO a_category (name, description, slug) VALUES (:name, :description, :slug)', $category); $oldIdToNewId[$category['id']] = $migrate->lastInsertId(); } } echo "Migrating from aBlogItemCategory to aBlogItemToCategory...\n"; if ($migrate->tableExists('a_blog_item_category')) { $itemIds = $migrate->query('SELECT id FROM a_blog_item'); $validItemIds = array(); foreach ($itemIds as $row) { $validItemIds[$row['id']] = true; } $oldMappings = $migrate->query('SELECT * FROM a_blog_item_category'); foreach ($oldMappings as $info) { $info['category_id'] = $oldIdToNewId[$info['blog_category_id']]; if (isset($validItemIds[$info['blog_item_id']])) { $migrate->query('INSERT INTO a_blog_item_to_category (blog_item_id, category_id) VALUES (:blog_item_id, :category_id)', $info); } } } if ($migrate->tableExists('a_blog_page_category')) { echo "Associating existing blog engine pages with new category IDs\n"; $itemIds = $migrate->query('SELECT id FROM a_page WHERE engine = "aBlog" OR engine = "aEvent"'); $validItemIds = array(); foreach ($itemIds as $row) { $validItemIds[$row['id']] = true; } $oldMappings = $migrate->query('SELECT * FROM a_blog_page_category'); foreach ($oldMappings as $info) { $info['category_id'] = $oldIdToNewId[$info['blog_category_id']]; if (isset($validItemIds[$info['page_id']])) { $migrate->query('INSERT INTO a_page_to_category (page_id, category_id) VALUES (:page_id, :category_id)', $info); } } } echo "Associating existing blog slots with new category IDs\n"; $blogSlots = $migrate->query('SELECT id, value FROM a_slot WHERE type = "aBlog" OR type = "aEvent"'); foreach ($blogSlots as $blogSlot) { $value = $blogSlot['value']; $info = @unserialize($value); if (isset($info['categories_list'])) { $new = array(); foreach ($info['categories_list'] as $categoryId) { if (isset($oldIdToNewId[$categoryId])) { $new[] = $oldIdToNewId[$categoryId]; } } $info['categories_list'] = $new; $blogSlot['value'] = serialize($info); $migrate->query('UPDATE a_slot SET value = :value WHERE id = :id', $blogSlot); } } // permissions regime for blogs $blogAdmin = new sfGuardPermission(); $blogAdmin->setName('blog_admin'); $blogAdmin->setDescription('Blog administration'); $blogAdmin->save(); $blogAuthor = new sfGuardPermission(); $blogAuthor->setName('blog_author'); $blogAuthor->setDescription('Blog post authoring'); $blogAuthor->save(); // Grant the expected access to the admin and editor groups if they are there $adminGroup = Doctrine::getTable('sfGuardGroup')->findOneByName('admin'); if ($adminGroup) { $adminGroup->Permissions[] = $blogAdmin; $adminGroup->save(); } $editorGroup = Doctrine::getTable('sfGuardGroup')->findOneByName('editor'); if ($editorGroup) { $editorGroup->Permissions[] = $blogAuthor; $editorGroup->save(); } } // Older updates may not have categories on the virtual page $blogPagesById = array(); $blogPageIdInfos = $migrate->query("SELECT id, page_id FROM a_blog_item"); foreach ($blogPageIdInfos as $info) { $blogPagesById[$info['id']] = $info['page_id']; } $blogToCategories = $migrate->query("SELECT * FROM a_blog_item_to_category"); foreach ($blogToCategories as $toCategory) { $migrate->query("INSERT INTO a_page_to_category (category_id, page_id) VALUES (:category_id, :page_id) ON DUPLICATE KEY UPDATE category_id = category_id", array('category_id' => $toCategory['category_id'], 'page_id' => $blogPagesById[$toCategory['blog_item_id']])); } // Older versions did not have taggings on the virtual page $blogTaggings = $migrate->query("SELECT * FROM tagging WHERE taggable_model IN ('aBlogPost', 'aEvent')"); $blogTagsById = array(); foreach ($blogTaggings as $tagging) { $blogTagsById[$tagging['taggable_id']][$tagging['tag_id']] = true; } $pageTaggings = $migrate->query("SELECT * FROM tagging WHERE taggable_model IN ('aPage')"); $pageTagsById = array(); foreach ($pageTaggings as $tagging) { $pageTagsById[$tagging['taggable_id']][$tagging['tag_id']] = true; } foreach ($blogTagsById as $blogId => $tags) { if (!isset($blogPagesById[$blogId])) { // No virtual page - just a stale tagging continue; } foreach ($tags as $tagId => $dummy) { if (!isset($pageTagsById[$blogPagesById[$blogId]][$tagId])) { $migrate->query('INSERT INTO tagging (taggable_model, taggable_id, tag_id) VALUES ("aPage", :taggable_id, :tag_id)', array('taggable_id' => $blogPagesById[$blogId], 'tag_id' => $tagId)); } } } $migrate->query('UPDATE a_page SET engine = "aBlog" WHERE slug LIKE "@a_blog_search_redirect%"'); $migrate->query('UPDATE a_page SET engine = "aEvent" WHERE slug LIKE "@a_event_search_redirect%"'); // Older blog post virtual pages won't have published_at $migrate->query('update a_page p inner join a_blog_item bi on bi.page_id = p.id set p.published_at = bi.published_at'); // Really old events may have full timestamps in start_date and end_date, break them out $migrate->query('UPDATE a_blog_item SET start_time = substr(start_date, 12), start_date = substr(start_date, 1, 10) WHERE (length(start_date) > 10) AND start_time IS NULL'); $migrate->query('ALTER TABLE a_blog_item modify column start_date date;'); $migrate->query('UPDATE a_blog_item SET end_time = substr(end_date, 12), end_date = substr(end_date, 1, 10) WHERE (length(end_date) > 10) AND end_time IS NULL'); $migrate->query('ALTER TABLE a_blog_item modify column end_date date;'); // Migrate old full day events from before we started defining this as a null start and end time $migrate->query('UPDATE a_blog_item SET start_time = null, end_time = null WHERE start_time = "00:00:00" AND end_time = "00:00:00"'); if ($migrate->tableExists('a_blog_category_user')) { $oldCategoryUsers = $migrate->query('SELECT * FROM a_blog_category_user'); $oldCategories = $migrate->query('SELECT * from a_blog_category'); $newCategories = $migrate->query('SELECT * from a_category'); $oldByName = array(); foreach ($oldCategories as $oldCategory) { $oldByName[$oldCategory['name']] = $oldCategory['id']; } $newByName = array(); foreach ($newCategories as $newCategory) { $newByName[$newCategory['name']] = $newCategory['id']; } $oldToNew = array(); foreach ($oldByName as $name => $id) { $oldToNew[$id] = $newByName[$name]; } foreach ($oldCategoryUsers as $oldCategoryUser) { $migrate->query('INSERT INTO a_category_user (category_id, user_id) VALUES (:category_id, :user_id) ON DUPLICATE KEY UPDATE category_id = category_id', array('category_id' => $oldToNew[$oldCategoryUser['blog_category_id']], 'user_id' => $oldCategoryUser['user_id'])); } } if ($migrate->tableExists('a_blog_category_group')) { $oldCategoryGroups = $migrate->query('SELECT * FROM a_blog_category_group'); $oldCategories = $migrate->query('SELECT * from a_blog_category'); $newCategories = $migrate->query('SELECT * from a_category'); $oldByName = array(); foreach ($oldCategories as $oldCategory) { $oldByName[$oldCategory['name']] = $oldCategory['id']; } $newByName = array(); foreach ($newCategories as $newCategory) { $newByName[$newCategory['name']] = $newCategory['id']; } $oldToNew = array(); foreach ($oldByName as $name => $id) { $oldToNew[$id] = $newByName[$name]; } foreach ($oldCategoryGroups as $oldCategoryGroup) { if (!isset($oldToNew[$oldCategoryGroup['blog_category_id']])) { echo "WARNING: there is no a_blog_category with the id " . $oldCategoryGroup['blog_category_id'] . "\n"; continue; } $migrate->query('INSERT INTO a_category_group (category_id, group_id) VALUES (:category_id, :group_id) ON DUPLICATE KEY UPDATE category_id = category_id', array('category_id' => $oldToNew[$oldCategoryGroup['blog_category_id']], 'group_id' => $oldCategoryGroup['group_id'])); } } // Blog item tags must also be on the virtual page, ditto for categories if (!$migrate->getCommandsRun()) { echo "Your database is already up to date.\n\n"; } else { echo $migrate->getCommandsRun() . " SQL commands were run.\n\n"; } echo "Done!\n"; }
/** * Filter the query by a related sfGuardPermission object * * @param sfGuardPermission $sfGuardPermission the related object to use as filter * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL * * @return sfGuardUserPermissionQuery The current query, for fluid interface */ public function filterBysfGuardPermission($sfGuardPermission, $comparison = null) { return $this->addUsingAlias(sfGuardUserPermissionPeer::PERMISSION_ID, $sfGuardPermission->getId(), $comparison); }
$sfGuardUser->setUsername('test'); sfFacebook::getGuardAdapter()->setUserProfileProperty($sfGuardUser, 'email_hash', sfFacebookConnect::getEmailHash('*****@*****.**')); try { $con = Doctrine::getConnectionByTableName('sfGuardUser'); $con->beginTransaction(); $sfGuardUser->save(); $sfGuardUser->getProfile()->save(); $con->commit(); $t->is(sfFacebook::getGuardAdapter()->getSfGuardUserByEmailHashes(array('trucmuche', sfFacebookConnect::getEmailHash('*****@*****.**')))->getUsername(), 'test'); } catch (Exception $e) { $con->rollback(); throw $e; } $sfGuardUser->delete(); $t->diag('sfFacebook::getGuardAdapter()->createSfGuardUserWithFacebookUid Test'); sfFacebook::getGuardAdapter()->createSfGuardUserWithFacebookUid(9999999999.0); $sfGuardUser = sfFacebook::getGuardAdapter()->getSfGuardUserByFacebookUid(9999999999.0); $t->is($sfGuardUser->getUsername(), 'Facebook_9999999999'); $sfGuardUser->delete(); $t->diag('sfFacebook::getGuardAdapter()->setDefaultPermissions Test'); $permission = new sfGuardPermission(); $permission->setName('member'); $permission->save(); sfConfig::set('app_facebook_connect_user_permissions', array('member')); $sfGuardUser = new sfGuardUser(); $sfGuardUser->setUsername('test'); $sfGuardUser->save(); sfFacebook::getGuardAdapter()->setDefaultPermissions($sfGuardUser); $t->is($sfGuardUser->getPermissionNames(), array('member')); $sfGuardUser->delete(); $permission->delete();
/** * Создать sfGuardUserPermission */ public function makeGuardUserPermission(sfGuardUser $user, sfGuardPermission $permission, $save = false, array $props = array()) { $defaultProps = array('user_id' => $user->getId(), 'permission_id' => $permission->getId()); $props = array_merge($defaultProps, $props); return $this->makeModel('sfGuardUserPermission', $props, $save); }
/** * Returns an array of name => id pairs representing the sfGuardPermission * objects that should be added to this menu item. * * This will create a new sfGuardPermission object if it does not exist. * * @param array $permissions The array of string permissions names to retrieve * @return array * @throws sfException */ protected function _fetchPermissionsArray($permissions) { foreach ($permissions as $permission) { if (is_array($permission)) { // The credentials are multi-level, implying the use of "or" logic. // This is not supported in the flat Permissions relation throw new sfException('Persisting credentials with "or" logic is not supported.'); } } // fetch the current permissions $currentPermissions = Doctrine_Query::create()->from('sfGuardPermission p INDEXBY p.name')->select('p.name, p.id')->whereIn('p.name', $permissions)->execute(null, Doctrine_Core::HYDRATE_ARRAY); // loop through the permissions and create any permissions that don't exist $permissionsArray = array(); foreach ($permissions as $permission) { if (isset($currentPermissions[$permission])) { $permissionsArray[$permission] = $currentPermissions[$permission]['id']; } else { // create a new permission object $guardPermission = new sfGuardPermission(); $guardPermission->name = $permission; $guardPermission->save(); $permissionsArray[$permission] = $guardPermission->id; } } return $permissionsArray; }