/** * Create new user */ public function actionCreate($name = 'admin', $email = '*****@*****.**', $role = 'admin', $password = '******') { // Make sure we have all the required values $required = array('name', 'email', 'role', 'password'); foreach ($required as $req) { if (!${$req}) { echoCli(sprintf('Please specify a value for the "%s" property or don\'t specify it at all.', $req)); return; } } // Check if the user exists by email and name $userExists = Yii::app()->db->createCommand(array('select' => array('id'), 'from' => 'user', 'where' => 'name=:name OR email=:email', 'params' => array(':name' => $name, ':email' => $email)))->queryRow(); // If exists error if ($userExists) { echoCli(sprintf("Sorry, That user with the email address or name already exists.")); return; } // Create the user Yii::app()->db->createCommand()->insert('user', array('created_at' => time(), 'name' => $name, 'email' => $email, 'role' => $role, 'password_hash' => User::hashPassword($password))); $lastID = Yii::app()->db->getLastInsertID(); // Assign the role to the user if (!Yii::app()->authManager->isAssigned($role, $lastID)) { $authItem = Yii::app()->authManager->getAuthItem($role); Yii::app()->authManager->assign($role, $lastID, $authItem->bizrule, $authItem->data); Yii::app()->authManager->assign('op_acp_access', $lastID, $authItem->bizrule, $authItem->data); } // Done echoCli('User Created!'); }
/** * Import permissions into the db */ public function actionImport($truncate = false, $overwrite = false, $filename = 'permissions.xml') { $fileLocation = Yii::getPathOfAlias('console.data') . '/' . $filename; if (!file_exists($fileLocation)) { echoCli(sprintf("Sorry, The file '%s' was not found.", $fileLocation), true); } if ($truncate) { // We first delete all the current settings and categories Yii::app()->db->createCommand('TRUNCATE TABLE `auth_item_child`; TRUNCATE TABLE `auth_item`')->execute(); // Reset the auto increment Yii::app()->db->createCommand('ALTER TABLE `auth_item_child` AUTO_INCREMENT=0;ALTER TABLE `auth_item` AUTO_INCREMENT=0;')->execute(); } $authItemsOld = Yii::app()->db->createCommand('SELECT * FROM `auth_item`')->queryAll(); // Auth items indexed by name $oldAuthItems = array(); foreach ($authItemsOld as $authItemOld) { $oldAuthItems[$authItemOld['name']] = $authItemOld; } $authItemsChildOld = Yii::app()->db->createCommand('SELECT * FROM `auth_item_child`')->queryAll(); // Auth items indexed by name $oldAuthItemChilds = array(); foreach ($authItemsChildOld as $authItemChildOld) { $oldAuthItemChilds[$authItemChildOld['parent'] . '_' . $authItemChildOld['child']] = $authItemChildOld; } // Load settings file $xml = new ClassXML(); $xml->loadXML(file_get_contents($fileLocation)); // Import categories echoCli('Adding Auth Items'); foreach ($xml->fetchElements('auth_item') as $authItem) { $data = $xml->fetchElementsFromRecord($authItem); if (isset($oldAuthItems[$data['name']])) { echoCli(sprintf('Auth Item "%s" Exists', $data['name'])); // Do we want to overwrite if ($overwrite) { echoCli(sprintf('-- Overwriting Auth Item "%s"', $data['name'])); // Update Yii::app()->db->createCommand()->update('auth_item', $data, 'name=:name', array(':name' => $data['name'])); } else { echoCli(sprintf('-- Skipping Auth Item "%s"', $data['name'])); } } else { echoCli(sprintf('Inserting Auth Item "%s"', $data['name'])); Yii::app()->db->createCommand()->insert('auth_item', $data); } } // Import settings echoCli('Adding Auth Item Childs'); foreach ($xml->fetchElements('auth_item_child_row') as $authItemChild) { $data = $xml->fetchElementsFromRecord($authItemChild); $childKey = $data['parent'] . '_' . $data['child']; if (isset($oldAuthItemChilds[$childKey])) { echoCli(sprintf('Auth Item Child "%s" Exists', $childKey)); // Do we want to overwrite if ($overwrite) { echoCli(sprintf('-- Overwriting Auth Item "%s"', $childKey)); // Update Yii::app()->db->createCommand()->update('auth_item_child', $data, 'parent=:parent AND child=:child', array(':parent' => $data['parent'], ':child' => $data['child'])); } else { echoCli(sprintf('-- Skipping Auth Item Child "%s"', $childKey)); } } else { echoCli(sprintf('Inserting Auth Item Child "%s"', $childKey)); Yii::app()->db->createCommand()->insert('auth_item_child', $data); } } echoCli('Import Done'); }
/** * Import settings into the db */ public function actionImport($truncate = false, $overwrite = false, $filename = 'settings.xml') { $fileLocation = Yii::getPathOfAlias('console.data') . '/' . $filename; if (!file_exists($fileLocation)) { echoCli(sprintf("Sorry, The file '%s' was not found.", $fileLocation), true); } if ($truncate) { // We first delete all the current settings and categories Yii::app()->db->createCommand('TRUNCATE TABLE `settingcat`; TRUNCATE TABLE `setting`')->execute(); // Reset the auto increment Yii::app()->db->createCommand('ALTER TABLE `settingcat` AUTO_INCREMENT=0;ALTER TABLE `setting` AUTO_INCREMENT=0;')->execute(); } // Get the current setting cats and settings $oldSettingCats = Yii::app()->db->createCommand('SELECT * FROM `settingcat`')->queryAll(); $oldSettings = Yii::app()->db->createCommand('SELECT * FROM `setting`')->queryAll(); // Setting categories indexed by category key $oldSettingCatsKeys = array(); foreach ($oldSettingCats as $oldSettingCat) { $oldSettingCatsKeys[$oldSettingCat['groupkey']] = $oldSettingCat; } // Settings indexed by setting key $oldSettingsKeys = array(); foreach ($oldSettings as $oldSetting) { $oldSettingsKeys[$oldSetting['settingkey']] = $oldSetting; } // Load settings file $xml = new ClassXML(); $xml->loadXML(file_get_contents($fileLocation)); // Import categories echoCli('Adding Setting Categories'); foreach ($xml->fetchElements('setting_category') as $category) { $data = $xml->fetchElementsFromRecord($category); if (isset($oldSettingCatsKeys[$data['groupkey']])) { echoCli(sprintf('Category "%s" Exists', $data['title'])); // Do we want to overwrite if ($overwrite) { echoCli(sprintf('-- Overwriting Category "%s"', $data['title'])); // Update Yii::app()->db->createCommand()->update('settingcat', $data, 'groupkey=:key', array(':key' => $data['groupkey'])); } else { echoCli(sprintf('-- Skipping Category "%s"', $data['title'])); } } else { echoCli(sprintf('Inserting Category "%s"', $data['title'])); Yii::app()->db->createCommand()->insert('settingcat', $data); } } // Grab the new categories $categories = Yii::app()->db->createCommand('SELECT * FROM `settingcat`')->queryAll(); $categoriesKeys = array(); foreach ($categories as $category) { $categoriesKeys[strtolower($category['groupkey'])] = $category['id']; } // Import settings echoCli('Adding Settings'); foreach ($xml->fetchElements('setting_row') as $setting) { $data = $xml->fetchElementsFromRecord($setting); // Unset value, value never changes unset($data['value']); // Grab new category value $data['category'] = $categoriesKeys[strtolower($data['category'])]; if (isset($oldSettingsKeys[$data['settingkey']])) { echoCli(sprintf('Setting "%s" Exists', $data['title'])); // Do we want to overwrite if ($overwrite) { echoCli(sprintf('-- Overwriting Setting "%s"', $data['title'])); // Update Yii::app()->db->createCommand()->update('setting', $data, 'settingkey=:key', array(':key' => $data['settingkey'])); } else { echoCli(sprintf('-- Skipping Setting "%s"', $data['title'])); } } else { echoCli(sprintf('Inserting Setting "%s"', $data['title'])); Yii::app()->db->createCommand()->insert('setting', $data); } } echoCli('Import Done'); }
/** * Import messages into the db */ public function actionImport($truncate = false, $overwrite = false, $filename = 'language_messages.xml') { $fileLocation = Yii::getPathOfAlias('console.data') . '/' . $filename; if (!file_exists($fileLocation)) { echoCli(sprintf("Sorry, The file '%s' was not found.", $fileLocation), true); } if ($truncate) { // We first delete all the current settings and categories Yii::app()->db->createCommand('TRUNCATE TABLE `source_message`;')->execute(); // Reset the auto increment Yii::app()->db->createCommand('ALTER TABLE `source_message` AUTO_INCREMENT=0;')->execute(); } // Get the current setting cats and settings $oldMessages = Yii::app()->db->createCommand('SELECT * FROM `source_message`')->queryAll(); // Setting categories indexed by category key $oldMessagesKeys = array(); foreach ($oldMessages as $oldMessage) { $oldMessagesKeys[sha1($oldMessage['category'] . '_' . $oldMessage['message'])] = $oldMessage; } // Load settings file $xml = new ClassXML(); $xml->loadXML(file_get_contents($fileLocation)); // Import categories echoCli('Adding Source Messages'); foreach ($xml->fetchElements('message_row') as $message) { $data = $xml->fetchElementsFromRecord($message); if (isset($oldMessagesKeys[sha1($data['category'] . '_' . $data['message'])])) { echoCli(sprintf('Message "%s" Exists', $data['message'])); // Do we want to overwrite if ($overwrite) { echoCli(sprintf('-- Overwriting Message "%s"', $data['category'] . '_' . $data['message'])); // Update Yii::app()->db->createCommand()->update('source_message', $data, 'category=:category AND message=:message', array(':category' => $data['category'], ':message' => $data['message'])); } else { echoCli(sprintf('-- Skipping Message "%s"', $data['category'] . '_' . $data['message'])); } } else { echoCli(sprintf('Inserting Message "%s"', $data['category'] . '_' . $data['message'])); Yii::app()->db->createCommand()->insert('source_message', $data); } } // Sync all languages echoCli('Syncing Languages'); $langs = Language::model()->findAll(); foreach ($langs as $lang) { echoCli('Syncing ' . $lang->name); $lang->SyncLanguageStrings(); } echoCli('Import Done'); }