/** * Retrieve contributor model by vcs type, contributorName, contributorEmail. * * If contributor registered at the system, returns it, if else - returns UnregisteredContributor model. * * @see UserAccount * @see User * * @param string $vcsType VCS type (git, hg, etc.) * @param string $contributorName VCS contributor name (e.g. commiter name) * @param string $contributorEmail VCS contributor e-mail (e.g. commiter e-mail, if exists) * * @return ContributorInterface Returns registered user, or UnregisteredContributor model. */ public function getContributor($vcsType, $contributorName, $contributorEmail = null) { /* @var $cached ContributorInterface[] */ static $cached = []; $cacheKey = $vcsType . '_' . $contributorName . '_' . $contributorEmail; if (!isset($cached[$cacheKey])) { /* @var $res ActiveQuery */ $res = User::find()->joinWith('accounts')->orWhere([UserAccount::tableName() . '.username' => $contributorName, UserAccount::tableName() . '.type' => $vcsType]); if (!empty($contributorEmail)) { $res->orWhere([User::tableName() . '.email' => $contributorEmail]); } $res->groupBy(User::tableName() . '.id'); $cached[$cacheKey] = $res->one(); if (!$cached[$cacheKey]) { // contributor not found // set as unregistered $cached[$cacheKey] = new UnregisteredContributor(['contributorName' => $contributorName, 'contributorEmail' => $contributorEmail]); } } return $cached[$cacheKey]; }
/** * Get user's VCS accounts * * @return ActiveQuery */ public function getAccounts() { return $this->hasMany(UserAccount::className(), ['user_id' => 'id']); }
/** * @inheritdoc */ public function rules() { return array_merge(parent::rules(), [['deletionFlag', 'boolean']]); }
/** * Retreive user model by user VCS bind account (UserAccount model). * * @see UserAccount * @see User * * @param string $vcsType VCS type (git, hg, etc.) * @param string $contributorName VCS contributor name (e.g. commiter name) * @param string $contributorEmail VCS contributor e-mail (e.g. commiter e-mail, if exists) * * @return User|null Returns user model if it exists */ public function getUserByUsername($vcsType, $contributorName, $contributorEmail = null) { /* @var $cached User[] */ static $cached = []; $cacheKey = $vcsType . '_' . $contributorName . '_' . $contributorEmail; if (!isset($cached[$cacheKey])) { /* @var $res ActiveQuery */ $res = User::find()->joinWith('accounts')->orWhere([UserAccount::tableName() . '.username' => $contributorName, UserAccount::tableName() . '.type' => $vcsType]); if (!empty($contributorEmail)) { $res->orWhere([User::tableName() . '.email' => $contributorEmail]); } $res->groupBy(User::tableName() . '.id'); $cached[$cacheKey] = $res->one(); } return $cached[$cacheKey]; }
/** * Test user accounts relations * * @depends testValidationAndSave * * @return User */ public function testUserAccounts() { /* @var $user User */ $user = $this->getModule('Yii2')->grabFixture('users', 'activeUser1'); $model = new UserAccount(); $attributes = ['user_id' => [['value' => null, 'isValid' => false], ['value' => [], 'isValid' => false], ['value' => 0, 'isValid' => false], ['value' => -100, 'isValid' => false], ['value' => $user->id, 'isValid' => true]], 'username' => [['value' => null, 'isValid' => false], ['value' => [], 'isValid' => false], ['value' => 0, 'isValid' => false], ['value' => str_repeat('A', 101), 'isValid' => false], ['value' => str_repeat('A', 100), 'isValid' => true]], 'type' => [['value' => null, 'isValid' => false], ['value' => [], 'isValid' => false], ['value' => 'wrong type', 'isValid' => false], ['value' => UserAccount::TYPE_HG, 'isValid' => true], ['value' => UserAccount::TYPE_SVN, 'isValid' => true], ['value' => UserAccount::TYPE_GIT, 'isValid' => true]]]; $this->getModule('\\Helper\\Unit')->validateModelAttributes($model, $attributes, $this); $this->assertTrue($model->save()); // check unique record with type and username columns $newModel = new UserAccount(); $newModel->setAttributes($model->getAttributes()); $this->assertFalse($newModel->validate()); $this->assertArrayHasKey('type', $newModel->getErrors()); $this->assertArrayHasKey('username', $newModel->getErrors()); $newModel->type = UserAccount::TYPE_HG; $this->assertTrue($newModel->validate()); $this->assertTrue($newModel->save()); $this->assertContainsOnly(UserAccount::className(), $user->accounts); $this->assertCount(2, $user->accounts); $this->assertInstanceOf(User::className(), $model->user); $this->assertEquals($user->id, $model->user->id); $this->assertInstanceOf(User::className(), $newModel->user); $this->assertEquals($user->id, $newModel->user->id); return $user; }