public function actionAuto() { $this->stdout("Install:\n\n", Console::FG_YELLOW); \humhub\modules\installer\libs\InitialData::bootstrap(); Yii::$app->settings->set('name', "HumHub Test"); Yii::$app->settings->set('mailer.systemEmailName', "*****@*****.**"); Yii::$app->settings->set('mailer.systemEmailName', "*****@*****.**"); Yii::$app->settings->set('secret', \humhub\libs\UUID::v4()); $user = new User(); //$user->group_id = 1; $user->username = "******"; $user->email = '*****@*****.**'; $user->status = User::STATUS_ENABLED; $user->language = ''; $user->last_activity_email = new \yii\db\Expression('NOW()'); if (!$user->save()) { throw new \yii\base\Exception("Could not save user"); } $user->profile->title = "System Administration"; $user->profile->firstname = "John"; $user->profile->lastname = "Doe"; $user->profile->save(); $password = new Password(); $password->user_id = $user->id; $password->setPassword('test'); $password->save(); // Assign to system admin group Group::getAdminGroup()->addUser($user); return self::EXIT_CODE_NORMAL; }
public function testAutoWallCreation() { $user = new User(); $user->username = "******"; $user->email = "*****@*****.**"; $this->assertTrue($user->save()); $this->assertNotNull($user->wall_id); $wall = Wall::findOne(['id' => $user->wall_id]); $this->assertNotNull($wall); }
public function getMailUpdate(User $user, $interval) { $output = ['html' => '', 'plaintext' => '']; $receive_email_activities = $user->getSetting("receive_email_activities", 'core', Setting::Get('receive_email_activities', 'mailing')); // User never wants activity content if ($receive_email_activities == User::RECEIVE_EMAIL_NEVER) { return ""; } // We are in hourly mode and user wants receive a daily summary if ($interval == CronController::EVENT_ON_HOURLY_RUN && $receive_email_activities == User::RECEIVE_EMAIL_DAILY_SUMMARY) { return ""; } // We are in daily mode and user wants receive not daily if ($interval == CronController::EVENT_ON_DAILY_RUN && $receive_email_activities != User::RECEIVE_EMAIL_DAILY_SUMMARY) { return ""; } // User is online and want only receive when offline if ($interval == CronController::EVENT_ON_HOURLY_RUN) { $isOnline = count($user->httpSessions) > 0; if ($receive_email_activities == User::RECEIVE_EMAIL_WHEN_OFFLINE && $isOnline) { return ""; } } $lastMailDate = $user->last_activity_email; if ($lastMailDate == "" || $lastMailDate == "0000-00-00 00:00:00") { $lastMailDate = new \yii\db\Expression('NOW() - INTERVAL 24 HOUR'); } $stream = new \humhub\modules\dashboard\components\actions\DashboardStream('stream', Yii::$app->controller); $stream->limit = 50; $stream->mode = \humhub\modules\content\components\actions\Stream::MODE_ACTIVITY; $stream->user = $user; $stream->init(); $stream->activeQuery->andWhere(['>', 'content.created_at', $lastMailDate]); foreach ($stream->getWallEntries() as $wallEntry) { try { $activity = $wallEntry->content->getPolymorphicRelation(); $output['html'] .= $activity->getActivityBaseClass()->render(BaseActivity::OUTPUT_MAIL); $output['plaintext'] .= $activity->getActivityBaseClass()->render(BaseActivity::OUTPUT_MAIL_PLAINTEXT); } catch (\yii\base\Exception $ex) { \Yii::error($ex->getMessage()); } } $user->last_activity_email = new \yii\db\Expression('NOW()'); $user->save(); return $output; }
/** * Registers a user * @param $data * @return Bool */ private function registerUser($data) { $userModel = new User(); $userModel->scenario = 'registration'; $profileModel = $userModel->profile; $profileModel->scenario = 'registration'; // User: Set values $userModel->username = $data['username']; $userModel->email = $data['email']; $userModel->group_id = $data['group_id']; $userModel->status = User::STATUS_ENABLED; // Profile: Set values $profileModel->firstname = $data['firstname']; $profileModel->lastname = $data['lastname']; // Password: Set values $userPasswordModel = new Password(); $userPasswordModel->setPassword($data['password']); if ($userModel->save()) { // Save user profile $profileModel->user_id = $userModel->id; $profileModel->save(); // Save user password $userPasswordModel->user_id = $userModel->id; $userPasswordModel->save(); // Join space / create then join space foreach ($data['space_names'] as $key => $space_name) { // Find space by name attribute $space = Space::findOne(['name' => $space_name]); // Create the space if not found if ($space == null) { $space = new Space(); $space->name = $space_name; $space->save(); } // Add member into space $space->addMember($userModel->id); } return true; } else { Yii::$app->session->setFlash('error', Html::errorSummary($userModel)); return false; } }
/** * Stores an authClient to an user record * * @param \yii\authclient\BaseClient $authClient * @param User $user */ public static function storeAuthClientForUser(ClientInterface $authClient, User $user) { $attributes = $authClient->getUserAttributes(); if ($authClient instanceof interfaces\PrimaryClient) { $user->auth_mode = $authClient->getId(); $user->save(); } else { $auth = Auth::findOne(['source' => $authClient->getId(), 'source_id' => $attributes['id']]); /** * Make sure authClient is not double assigned */ if ($auth !== null && $auth->user_id != $user->id) { $auth->delete(); $auth = null; } if ($auth === null) { $auth = new \humhub\modules\user\models\Auth(['user_id' => $user->id, 'source' => (string) $authClient->getId(), 'source_id' => (string) $attributes['id']]); $auth->save(); } } }
/** * Updates or creates user by given ldap node * * @param Zend_Ldap_Node $node * @return User User Object */ public function handleLdapUser($node) { $usernameAttribute = Setting::Get('usernameAttribute', 'authentication_ldap'); if ($usernameAttribute == '') { $usernameAttribute = 'sAMAccountName'; } $emailAttribute = Setting::Get('emailAttribute', 'authentication_ldap'); if ($emailAttribute == '') { $emailAttribute = 'mail'; } $username = $node->getAttribute($usernameAttribute, 0); $email = $node->getAttribute($emailAttribute, 0); $guid = $this->binToStrGuid($node->getAttribute('objectGUID', 0)); // Try to load User: $userChanged = false; $user = null; if ($guid != "") { $user = User::findOne(array('guid' => $guid, 'auth_mode' => User::AUTH_MODE_LDAP)); } else { // Fallback use e-mail $user = User::findOne(array('email' => $email, 'auth_mode' => User::AUTH_MODE_LDAP)); } if ($user === null) { $user = new User(); if ($guid != "") { $user->guid = $guid; } $user->status = User::STATUS_ENABLED; $user->auth_mode = User::AUTH_MODE_LDAP; $user->group_id = 1; Yii::info('Create ldap user ' . $username . '!'); } // Update Users Field if ($user->username != $username) { $userChanged = true; $user->username = $username; } if ($user->email != $email) { $userChanged = true; $user->email = $email; } if ($user->validate()) { // Only Save user when something is changed if ($userChanged || $user->isNewRecord) { $user->save(); } // Update Profile Fields foreach (ProfileField::find()->andWhere(['!=', 'ldap_attribute', ''])->all() as $profileField) { $ldapAttribute = $profileField->ldap_attribute; $profileFieldName = $profileField->internal_name; $user->profile->{$profileFieldName} = $node->getAttribute($ldapAttribute, 0); } if ($user->profile->validate() && $user->profile->save()) { $this->trigger(self::EVENT_UPDATE_USER, new ParameterEvent(['user' => $user, 'node' => $node])); } else { Yii::error('Could not create or update ldap user profile! (' . print_r($user->profile->getErrors(), true) . ")"); } } else { Yii::error('Could not create or update ldap user! (' . print_r($user->getErrors(), true) . ")"); } return $user; }
/** * Updates or creates user by given ldap node * * @param Zend_Ldap_Node $node * @return User User Object */ public function handleLdapUser($node) { $username = $node->getAttribute(Setting::Get('usernameAttribute', 'authentication_ldap'), 0); $email = $node->getAttribute('mail', 0); $guid = $this->binToStrGuid($node->getAttribute('objectGUID', 0)); // Try to load User: $userChanged = false; $user = null; if ($guid != "") { $user = User::findOne(array('guid' => $guid, 'auth_mode' => User::AUTH_MODE_LDAP)); } else { // Fallback use e-mail $user = User::findOne(array('email' => $email, 'auth_mode' => User::AUTH_MODE_LDAP)); } if ($user === null) { $user = new User(); if ($guid != "") { $user->guid = $guid; } $user->status = User::STATUS_ENABLED; $user->auth_mode = User::AUTH_MODE_LDAP; $user->group_id = 1; Yii::info('Create ldap user ' . $username . '!'); } // Update Group Mapping foreach (Group::find()->andWhere(['!=', 'ldap_dn', ""])->all() as $group) { if (in_array($group->ldap_dn, $node->getAttribute('memberOf'))) { if ($user->group_id != $group->id) { $userChanged = true; $user->group_id = $group->id; } } } // Update Users Field if ($user->username != $username) { $userChanged = true; $user->username = $username; } if ($user->email != $email) { $userChanged = true; $user->email = $email; } if ($user->validate()) { // Only Save user when something is changed if ($userChanged || $user->isNewRecord) { $user->save(); } // Update Profile Fields foreach (ProfileField::find()->andWhere(['!=', 'ldap_attribute', ''])->all() as $profileField) { $ldapAttribute = $profileField->ldap_attribute; $profileFieldName = $profileField->internal_name; $user->profile->{$profileFieldName} = $node->getAttribute($ldapAttribute, 0); } if ($user->profile->validate()) { $user->profile->save(); // Update Space Mapping foreach (Space::find()->andWhere(['!=', 'ldap_dn', ''])->all() as $space) { if (in_array($space->ldap_dn, $node->getAttribute('memberOf'))) { $space->addMember($user->id); } } } else { Yii::error('Could not create or update ldap user profile! (' . print_r($user->profile->getErrors(), true) . ")"); } } else { Yii::error('Could not create or update ldap user! (' . print_r($user->getErrors(), true) . ")"); } return $user; }
/** * Sample Data */ public function actionSampleData() { if (Yii::$app->getModule('installer')->settings->get('sampleData') == 1) { // Sample Data already created return $this->redirect(Yii::$app->getModule('installer')->getNextConfigStepUrl()); } $form = new \humhub\modules\installer\forms\SampleDataForm(); $form->sampleData = 1; if ($form->load(Yii::$app->request->post()) && $form->validate()) { Yii::$app->getModule('installer')->settings->set('sampleData', $form->sampleData); if (Yii::$app->getModule('installer')->settings->get('sampleData') == 1) { // Add sample image to admin $admin = User::find()->where(['id' => 1])->one(); $adminImage = new \humhub\libs\ProfileImage($admin->guid); $adminImage->setNew(Yii::getAlias("@webroot/resources/installer/user_male_1.jpg")); // Create second user $userModel = new User(); $userModel->scenario = 'registration'; $profileModel = $userModel->profile; $profileModel->scenario = 'registration'; $userModel->status = User::STATUS_ENABLED; $userModel->username = "******"; $userModel->email = "*****@*****.**"; $userModel->language = ''; $userModel->tags = "Microsoft Office, Marketing, SEM, Digital Native"; $userModel->last_activity_email = new \yii\db\Expression('NOW()'); $userModel->save(); $profileImage = new \humhub\libs\ProfileImage($userModel->guid); $profileImage->setNew(Yii::getAlias("@webroot/resources/installer/user_male_2.jpg")); $profileModel->user_id = $userModel->id; $profileModel->firstname = "David"; $profileModel->lastname = "Roberts"; $profileModel->title = "Late riser"; $profileModel->street = "2443 Queens Lane"; $profileModel->zip = "24574"; $profileModel->city = "Allwood"; $profileModel->country = "Virginia"; $profileModel->save(); // Create third user $userModel2 = new User(); $userModel2->scenario = 'registration'; $profileModel2 = $userModel2->profile; $profileModel2->scenario = 'registration'; $userModel2->status = User::STATUS_ENABLED; $userModel2->username = "******"; $userModel2->email = "*****@*****.**"; $userModel2->language = ''; $userModel2->tags = "Yoga, Travel, English, German, French"; $userModel2->last_activity_email = new \yii\db\Expression('NOW()'); $userModel2->save(); $profileImage2 = new \humhub\libs\ProfileImage($userModel2->guid); $profileImage2->setNew(Yii::getAlias("@webroot/resources/installer/user_female_1.jpg")); $profileModel2->user_id = $userModel2->id; $profileModel2->firstname = "Sara"; $profileModel2->lastname = "Schuster"; $profileModel2->title = "Do-gooder"; $profileModel2->street = "Schmarjestrasse 51"; $profileModel2->zip = "17095"; $profileModel2->city = "Friedland"; $profileModel2->country = "Niedersachsen"; $profileModel2->save(); // Switch Identity $user = User::find()->where(['id' => 1])->one(); Yii::$app->user->switchIdentity($user); $space = Space::find()->where(['id' => 1])->one(); // Create a sample post $post = new \humhub\modules\post\models\Post(); $post->message = Yii::t("InstallerModule.controllers_ConfigController", "We're looking for great slogans of famous brands. Maybe you can come up with some samples?"); $post->content->container = $space; $post->content->visibility = \humhub\modules\content\models\Content::VISIBILITY_PRIVATE; $post->save(); // Switch Identity Yii::$app->user->switchIdentity($userModel); $comment = new \humhub\modules\comment\models\Comment(); $comment->message = Yii::t("InstallerModule.controllers_ConfigController", "Nike – Just buy it. ;Wink;"); $comment->object_model = $post->className(); $comment->object_id = $post->getPrimaryKey(); $comment->save(); // Switch Identity Yii::$app->user->switchIdentity($userModel2); $comment2 = new \humhub\modules\comment\models\Comment(); $comment2->message = Yii::t("InstallerModule.controllers_ConfigController", "Calvin Klein – Between love and madness lies obsession."); $comment2->object_model = $post->className(); $comment2->object_id = $post->getPrimaryKey(); $comment2->save(); // Create Like Object $like = new \humhub\modules\like\models\Like(); $like->object_model = $comment->className(); $like->object_id = $comment->getPrimaryKey(); $like->save(); $like = new \humhub\modules\like\models\Like(); $like->object_model = $post->className(); $like->object_id = $post->getPrimaryKey(); $like->save(); // trigger install sample data event $this->trigger(self::EVENT_INSTALL_SAMPLE_DATA); } return $this->redirect(Yii::$app->getModule('installer')->getNextConfigStepUrl()); } return $this->render('sample-data', array('model' => $form)); }