public function processRecord($record, $columnMap, &$results, $preview = false)
 {
     // We match by 'Code', the ID property is confusing the importer
     if (isset($record['ID'])) {
         unset($record['ID']);
     }
     $objID = parent::processRecord($record, $columnMap, $results, $preview);
     $group = DataObject::get_by_id($this->objectClass, $objID);
     // set group hierarchies - we need to do this after all records
     // are imported to avoid missing "early" references to parents
     // which are imported later on in the CSV file.
     if (isset($record['ParentCode']) && $record['ParentCode']) {
         $parentGroup = DataObject::get_one('Group', array('"Group"."Code"' => $record['ParentCode']));
         if ($parentGroup) {
             $group->ParentID = $parentGroup->ID;
             $group->write();
         }
     }
     // set permission codes - these are all additive, meaning
     // existing permissions arent cleared.
     if (isset($record['PermissionCodes']) && $record['PermissionCodes']) {
         foreach (explode(',', $record['PermissionCodes']) as $code) {
             $p = DataObject::get_one('Permission', array('"Permission"."Code"' => $code, '"Permission"."GroupID"' => $group->ID));
             if (!$p) {
                 $p = new Permission(array('Code' => $code));
                 $p->write();
             }
             $group->Permissions()->add($p);
         }
     }
     return $objID;
 }
 public function onAfterWrite()
 {
     parent::onAfterWrite();
     // TODO: should test if this is needed or not
     if (!$this->owner->ID) {
         return;
     }
     // Apply the subsite title to config
     $siteconfig = $this->getSiteConfig();
     if ($siteconfig) {
         if ($siteconfig->Title == _t('Subsite.SiteConfigTitle', 'Your Site Name') && $this->owner->Title) {
             $siteconfig->Title = $this->owner->Title;
             $siteconfig->write();
         }
     }
     // Make sure we have groups for this subsite
     $groupName = $this->getAdministratorGroupName();
     $group = self::getGroupByName($groupName);
     if ($groupName && !$group) {
         $group = new Group();
         $group->Title = $groupName;
         $group->AccessAllSubsites = false;
         $group->write();
         $group->Subsites()->add($this->owner);
         // Apply default permissions to this group
         $codes = array_unique(array_keys(Permission::get_codes(false)));
         $default_permissions = Config::inst()->get('SubsiteExtension', 'admin_default_permissions');
         foreach ($default_permissions as $p) {
             if (in_array($p, $codes)) {
                 $po = new Permission(array('Code' => $p));
                 $po->write();
                 $group->Permissions()->add($po);
             }
         }
         $group->write();
     }
     $membersGroupName = $this->getMembersGroupName();
     $membersGroup = self::getGroupByName($membersGroupName);
     if ($membersGroupName && !$membersGroup) {
         $membersGroup = new Group();
         $membersGroup->Title = $membersGroupName;
         $membersGroup->AccessAllSubsites = true;
         $membersGroup->write();
         $membersGroup->Subsites()->add($this->owner);
         $membersGroup->write();
     }
 }
 /**
  * Create permissions, groups and member records if they don't exist.
  */
 public function requireDefaultRecords()
 {
     parent::requireDefaultRecords();
     $groups = array();
     // create or update groups, cache id by title
     foreach (self::$groups as $title => $description) {
         if (!($group = DataObject::get_one('Group', " Title = '{$title}'"))) {
             $group = new Group(array('Title' => $title));
         }
         // update description if exists, otherwise set
         $group->Description = $description;
         $group->write();
         $groups[$title] = $group->ID;
     }
     // create or update permissions and assign to associated group
     foreach (self::$permissions as $code => $groupTitle) {
         if (!($perm = DataObject::get_one('Permission', " Code = '{$code}' "))) {
             $perm = new Permission(array('Code' => $code));
         }
         $perm->GroupID = $groups[$groupTitle];
         $perm->write();
     }
     // if config option is true create or update members, then add Member to group
     if ($this->config()->get('create_members_and_assign_to_groups') === true) {
         foreach (self::$members as $memberInfo) {
             $email = $memberInfo['Email'];
             if (!($member = DataObject::get_one('Member', " Email = '{$email}' "))) {
                 $member = new Member();
             }
             // set or update data
             $member->update($memberInfo);
             $member->write();
             foreach (self::$member_groups[$email] as $groupTitle) {
                 // if not in the group already add it
                 $groupID = $groups[$groupTitle];
                 if (!$member->Groups()->filter('ID', $groupID)->first()) {
                     $member->Groups()->add($groupID);
                 }
                 $member->write();
             }
         }
     }
 }
Exemple #4
0
 /**
  * Deny the given permission code/arg to the given group
  *
  * @param int $groupID The ID of the group
  * @param string $code The permission code
  * @param string Optional: The permission argument (e.g. a page ID).
  * @returns Permission Returns the new permission object.
  */
 public static function deny($groupID, $code, $arg = "any")
 {
     $perm = new Permission();
     $perm->GroupID = $groupID;
     $perm->Code = $code;
     $perm->Type = self::DENY_PERMISSION;
     // Arg component
     switch ($arg) {
         case "any":
             break;
         case "all":
             $perm->Arg = -1;
         default:
             if (is_numeric($arg)) {
                 $perm->Arg = $arg;
             } else {
                 user_error("Permission::checkMember: bad arg '{$arg}'", E_USER_ERROR);
             }
     }
     $perm->write();
     return $perm;
 }
 /**
  * Create a member and group with the given permission code, and log in with it.
  * Returns the member ID.
  */
 function logInWithPermission($permCode = "ADMIN")
 {
     if (!isset($this->cache_generatedMembers[$permCode])) {
         $group = new Group();
         $group->Title = "{$permCode} group";
         $group->write();
         $permission = new Permission();
         $permission->Code = $permCode;
         $permission->write();
         $group->Permissions()->add($permission);
         $member = DataObject::get_one('Member', sprintf('"Email" = \'%s\'', "{$permCode}@example.org"));
         if (!$member) {
             $member = new Member();
         }
         $member->FirstName = $permCode;
         $member->Surname = "User";
         $member->Email = "{$permCode}@example.org";
         $member->write();
         $group->Members()->add($member);
         $this->cache_generatedMembers[$permCode] = $member;
     }
     $this->cache_generatedMembers[$permCode]->logIn();
 }
 /**
  * Creates the default code bank config
  */
 public function requireDefaultRecords()
 {
     parent::requireDefaultRecords();
     $codeVersion = singleton('CodeBank')->getVersion();
     if (!CodeBankConfig::get()->first()) {
         $conf = new CodeBankConfig();
         $conf->Version = $codeVersion;
         $conf->write();
         DB::alteration_message('Default Code Bank Config Created', 'created');
     }
     if (!Group::get()->filter('Code', 'code-bank-api')->first()) {
         $group = new Group();
         $group->Title = 'Code Bank Users';
         $group->Description = 'Code Bank Access Group';
         $group->Code = 'code-bank-api';
         $group->write();
         $permission = new Permission();
         $permission->Code = 'CODE_BANK_ACCESS';
         $permission->Type = 1;
         $permission->GroupID = $group->ID;
         $permission->write();
         DB::alteration_message('Code Bank Users Group Created', 'created');
     }
     //Check for and perform any needed updates
     $codeVersionTmp = explode(' ', $codeVersion);
     $dbVerTmp = explode(' ', CodeBankConfig::CurrentConfig()->Version);
     if ($codeVersionTmp[0] != '@@VERSION@@' && $codeVersionTmp[0] != $dbVerTmp[0]) {
         $updateXML = simplexml_load_string(file_get_contents('http://update.edchipman.ca/codeBank/airUpdate.xml'));
         $latestVersion = strip_tags($updateXML->version->asXML());
         $versionTmp = explode(' ', $latestVersion);
         //Sanity Check code version against latest
         if (version_compare($codeVersionTmp[0], $versionTmp[0], '>')) {
             DB::alteration_message('Unknown Code Bank server version ' . $codeVersion . ', current version available for download is ' . $latestVersion, 'error');
             return;
         }
         //Sanity Check make sure latest version is installed
         if ($codeVersionTmp[0] != $versionTmp[0]) {
             DB::alteration_message('A Code Bank Server update is available, please <a href="http://programs.edchipman.ca/applications/code-bank/">download</a> and install the update then run dev/build again.', 'error');
             return;
         }
         //Sanity Check database version against latest
         if (version_compare($dbVerTmp[0], $versionTmp[0], '<')) {
             $data = array('version' => CodeBankConfig::CurrentConfig()->Version, 'db_type' => 'SERVER');
             $data = http_build_query($data);
             $context = stream_context_create(array('http' => array('method' => 'POST', 'header' => "Content-type: application/x-www-form-urlencoded\r\n" . "Content-Length: " . strlen($data) . "\r\n", 'content' => $data)));
             //Download and run queries needed
             $sql = simplexml_load_string(file_get_contents('http://update.edchipman.ca/codeBank/DatabaseUpgrade.php', false, $context));
             $sets = count($sql->query);
             foreach ($sql->query as $query) {
                 $queries = explode('$', $query);
                 $t = count($queries);
                 foreach ($queries as $query) {
                     if (empty($query)) {
                         continue;
                     }
                     DB::query($query);
                 }
             }
             //Update Database Version
             $codeBankConfig = CodeBankConfig::CurrentConfig();
             $codeBankConfig->Version = $latestVersion;
             $codeBankConfig->write();
             DB::alteration_message('Code Bank Server database upgraded', 'changed');
         }
     }
 }
 /**
  * Create a member and group with the given permission code, and log in with it.
  * Returns the member ID.
  */
 function logInWithPermssion($permCode = "ADMIN")
 {
     if (!isset($this->cache_generatedMembers[$permCode])) {
         $group = new Group();
         $group->Title = "{$permCode} group";
         $group->write();
         $permission = new Permission();
         $permission->Code = $permCode;
         $permission->write();
         $group->Permissions()->add($permission);
         $member = new Member();
         $member->FirstName = $permCode;
         $member->Surname = "User";
         $member->Email = "{$permCode}@example.org";
         $member->write();
         $group->Members()->add($member);
         $this->cache_generatedMembers[$permCode] = $member;
     }
     $this->cache_generatedMembers[$permCode]->logIn();
 }