public function CreateAdministrator($sAdminUser, $sAdminPwd, $sLanguage = 'EN US')
 {
     CMDBObject::SetTrackInfo('Initialization');
     $oChange = CMDBObject::GetCurrentChange();
     $iContactId = 0;
     // Support drastic data model changes: no organization class (or not writable)!
     if (MetaModel::IsValidClass('Organization') && !MetaModel::IsAbstract('Organization')) {
         $oOrg = new Organization();
         $oOrg->Set('name', 'My Company/Department');
         $oOrg->Set('code', 'SOMECODE');
         $iOrgId = $oOrg->DBInsertTrackedNoReload($oChange, true);
         // Support drastic data model changes: no Person class  (or not writable)!
         if (MetaModel::IsValidClass('Person') && !MetaModel::IsAbstract('Person')) {
             $oContact = new Person();
             $oContact->Set('name', 'My last name');
             $oContact->Set('first_name', 'My first name');
             if (MetaModel::IsValidAttCode('Person', 'org_id')) {
                 $oContact->Set('org_id', $iOrgId);
             }
             if (MetaModel::IsValidAttCode('Person', 'phone')) {
                 $oContact->Set('phone', '+00 000 000 000');
             }
             $oContact->Set('email', '*****@*****.**');
             $iContactId = $oContact->DBInsertTrackedNoReload($oChange, true);
         }
     }
     $oUser = new UserLocal();
     $oUser->Set('login', $sAdminUser);
     $oUser->Set('password', $sAdminPwd);
     if (MetaModel::IsValidAttCode('UserLocal', 'contactid') && $iContactId != 0) {
         $oUser->Set('contactid', $iContactId);
     }
     $oUser->Set('language', $sLanguage);
     // Language was chosen during the installation
     // Add this user to the very specific 'admin' profile
     $oAdminProfile = MetaModel::GetObjectFromOQL("SELECT URP_Profiles WHERE name = :name", array('name' => ADMIN_PROFILE_NAME), true);
     if (is_object($oAdminProfile)) {
         $oUserProfile = new URP_UserProfile();
         //$oUserProfile->Set('userid', $iUserId);
         $oUserProfile->Set('profileid', $oAdminProfile->GetKey());
         $oUserProfile->Set('reason', 'By definition, the administrator must have the administrator profile');
         //$oUserProfile->DBInsertTrackedNoReload($oChange, true /* skip security */);
         $oSet = DBObjectSet::FromObject($oUserProfile);
         $oUser->Set('profile_list', $oSet);
     }
     $iUserId = $oUser->DBInsertTrackedNoReload($oChange, true);
     return true;
 }
 public function CreateAdministrator($sAdminUser, $sAdminPwd, $sLanguage = 'EN US')
 {
     // Create a change to record the history of the User object
     $oChange = MetaModel::NewObject("CMDBChange");
     $oChange->Set("date", time());
     $oChange->Set("userinfo", "Initialization");
     $iChangeId = $oChange->DBInsert();
     $oOrg = new Organization();
     $oOrg->Set('name', 'My Company/Department');
     $oOrg->Set('code', 'SOMECODE');
     //		$oOrg->Set('status', 'implementation');
     //$oOrg->Set('parent_id', xxx);
     $iOrgId = $oOrg->DBInsertTrackedNoReload($oChange, true);
     // Location : optional
     //$oLocation = new bizLocation();
     //$oLocation->Set('name', 'MyOffice');
     //$oLocation->Set('status', 'implementation');
     //$oLocation->Set('org_id', $iOrgId);
     //$oLocation->Set('severity', 'high');
     //$oLocation->Set('address', 'my building in my city');
     //$oLocation->Set('country', 'my country');
     //$oLocation->Set('parent_location_id', xxx);
     //$iLocationId = $oLocation->DBInsertNoReload();
     $oContact = new Person();
     $oContact->Set('name', 'My last name');
     $oContact->Set('first_name', 'My first name');
     //$oContact->Set('status', 'available');
     $oContact->Set('org_id', $iOrgId);
     $oContact->Set('email', '*****@*****.**');
     //$oContact->Set('phone', '');
     //$oContact->Set('location_id', $iLocationId);
     //$oContact->Set('employee_number', '');
     $iContactId = $oContact->DBInsertTrackedNoReload($oChange, true);
     $oUser = new UserLocal();
     $oUser->Set('login', $sAdminUser);
     $oUser->Set('password', $sAdminPwd);
     $oUser->Set('contactid', $iContactId);
     $oUser->Set('language', $sLanguage);
     // Language was chosen during the installation
     $iUserId = $oUser->DBInsertTrackedNoReload($oChange, true);
     // Add this user to the very specific 'admin' profile
     $oUserProfile = new URP_UserProfile();
     $oUserProfile->Set('userid', $iUserId);
     $oUserProfile->Set('profileid', ADMIN_PROFILE_ID);
     $oUserProfile->Set('reason', 'By definition, the administrator must have the administrator profile');
     $oUserProfile->DBInsertTrackedNoReload($oChange, true);
     return true;
 }
 protected static function SetProfilesFromCAS($oUser, $aGroups)
 {
     if (!MetaModel::IsValidClass('URP_Profiles')) {
         phpCAS::log("URP_Profiles is not a valid class. Automatic creation of Users is not supported in this context, sorry.");
         return false;
     }
     // read all the existing profiles
     $oProfilesSearch = new DBObjectSearch('URP_Profiles');
     $oProfilesSet = new DBObjectSet($oProfilesSearch);
     $aAllProfiles = array();
     while ($oProfile = $oProfilesSet->Fetch()) {
         $aAllProfiles[strtolower($oProfile->GetName())] = $oProfile->GetKey();
     }
     // Translate the CAS/LDAP group names into iTop profile names
     $aProfiles = array();
     $sPattern = MetaModel::GetConfig()->Get('cas_profile_pattern');
     foreach ($aGroups as $sGroupName) {
         if (preg_match($sPattern, $sGroupName, $aMatches)) {
             if (array_key_exists(strtolower($aMatches[1]), $aAllProfiles)) {
                 $aProfiles[] = $aAllProfiles[strtolower($aMatches[1])];
                 phpCAS::log("Info: Adding the profile '{$aMatches[1]}' from CAS.");
             } else {
                 phpCAS::log("Warning: {$aMatches[1]} is not a valid iTop profile (extracted from group name: '{$sGroupName}'). Ignored.");
             }
         } else {
             phpCAS::log("Info: The CAS group '{$sGroupName}' does not seem to match an iTop pattern. Ignored.");
         }
     }
     if (count($aProfiles) == 0) {
         phpCAS::log("Info: The user '" . $oUser->GetName() . "' has no profiles retrieved from CAS. Default profile(s) will be used.");
         // Second attempt: check if there is/are valid default profile(s)
         $sCASDefaultProfiles = MetaModel::GetConfig()->Get('cas_default_profiles');
         $aCASDefaultProfiles = explode(';', $sCASDefaultProfiles);
         foreach ($aCASDefaultProfiles as $sDefaultProfileName) {
             if (array_key_exists(strtolower($sDefaultProfileName), $aAllProfiles)) {
                 $aProfiles[] = $aAllProfiles[strtolower($sDefaultProfileName)];
                 phpCAS::log("Info: Adding the default profile '" . $aAllProfiles[strtolower($sDefaultProfileName)] . "' from CAS.");
             } else {
                 phpCAS::log("Warning: the default profile {$sDefaultProfileName} is not a valid iTop profile. Ignored.");
             }
         }
         if (count($aProfiles) == 0) {
             phpCAS::log("Error: The user '" . $oUser->GetName() . "' has no profiles in iTop, and therefore cannot be created.");
             return false;
         }
     }
     // Now synchronize the profiles
     $oProfilesSet = DBObjectSet::FromScratch('URP_UserProfile');
     foreach ($aProfiles as $iProfileId) {
         $oLink = new URP_UserProfile();
         $oLink->Set('profileid', $iProfileId);
         $oLink->Set('reason', 'CAS/LDAP Synchro');
         $oProfilesSet->AddObject($oLink);
     }
     $oUser->Set('profile_list', $oProfilesSet);
     phpCAS::log("Info: the user '" . $oUser->GetName() . "' (id=" . $oUser->GetKey() . ") now has the following profiles: '" . implode("', '", $aProfiles) . "'.");
     if ($oUser->IsModified()) {
         $oMyChange = MetaModel::NewObject("CMDBChange");
         $oMyChange->Set("date", time());
         $oMyChange->Set("userinfo", 'CAS/LDAP Synchro');
         $oMyChange->DBInsert();
         if ($oUser->IsNew()) {
             $oUser->DBInsertTracked($oMyChange);
         } else {
             $oUser->DBUpdateTracked($oMyChange);
         }
     }
     return true;
 }