/** * case: * * $data = [ * 'UserIdentifier1' => 'ClientIdentifier1', * 'UserIdentifier2' => 'ClientIdentifier2' * ] * * @param array $data * @return boolean */ public function bindUserClient(array $data) { $clientIdentifiers = array_values($data); $clientsInDb = ArrayHelper::getColumn(ClientModel::findByClientId($clientIdentifiers)->all(), 'id'); if (count(array_diff($clientIdentifiers, $clientsInDb)) !== 0) { return false; } $db = CommonModel::getDb(); $transaction = $db->beginTransaction(); try { foreach ($data as $user => $client) { $userClientModel = new UserClientsModel(); $userClientModel->user_id = $user; $userClientModel->client_id = $client; $userClientModel->save(); } $transaction->commit(); return true; } catch (\Exception $e) { $transaction->rollBack(); return false; } }
/** * case: * * $data = [ * 'clientIdentifier1' => 'scopeIdentifier1', * 'clientIdentifier2' => 'scopeIdentifier2' * ] * * @param array $data * @return boolean */ public function bindClientScope(array $data) { $clientIdentifiers = array_keys($data); $scopeIdentifiers = array_values($data); $clientsInDb = ArrayHelper::getColumn(ClientModel::findByClientId($clientIdentifiers)->all(), 'id'); $scopesInDb = ArrayHelper::getColumn(ScopesModel::findByScopeId($scopeIdentifiers)->all(), 'id'); if (count(array_diff($clientIdentifiers, $clientsInDb)) !== 0 or count(array_diff($scopeIdentifiers, $scopesInDb)) !== 0) { return false; } $db = CommonModel::getDb(); $transaction = $db->beginTransaction(); try { foreach ($data as $client => $scope) { $clientScopeModel = new ClientScopesModel(); $clientScopeModel->client_id = $client; $clientScopeModel->grant_id = $scope; $clientScopeModel->save(); } $transaction->commit(); return true; } catch (\Exception $e) { $transaction->rollBack(); return false; } }
/** * @return ActiveQuery */ public function getClient() { return $this->hasOne(ClientModel::className(), ['id' => 'client_id']); }