9 class Accounts
extends Table
11 protected $rowset = "app\\Model\\AccountCollection";
14 * @var \app\Model\Tokens
19 * @var \app\Model\Owners
23 function __construct(Connection
$conn, Tokens
$tokens, Owners
$owners) {
24 parent
::__construct("accounts", $conn);
25 $this->tokens
= $tokens;
26 $this->owners
= $owners;
29 function getTokens() {
33 function getOwners() {
37 function byOAuth($authority, $token, $user) {
38 if (!($account = $this->byOAuthToken($authority, $token))) {
39 $account = $this->byOAuthOwner($authority, $user);
44 function byOAuthToken($authority, $access_token, &$token = null) {
45 $tokens = $this->tokens
->find([
46 "token=" => $access_token,
47 "authority=" => $authority,
51 $token = $tokens->current();
52 $accounts = $this->by($token);
53 if (count($accounts)) {
54 return $accounts->current();
59 function byOAuthOwner($authority, $login, &$owner = null) {
60 $owners = $this->owners
->find([
61 "authority=" => $authority,
66 $owner = $owners->current();
67 $accounts = $this->by($owner);
68 if (count($accounts)) {
69 return $accounts->current();
74 function createOAuthAccount($authority, $token, $user) {
75 $account = $this->create([
76 "account" => new Expr("DEFAULT")
78 $this->owners
->create([
79 "account" => $account->account
,
80 "authority" => $authority,
83 $this->tokens
->create([
84 "account" => $account->account
,
85 "authority" => "github",