MediaWiki  1.31.0
OATHUserRepository.php
Go to the documentation of this file.
1 <?php
21 
24  protected $lb;
25 
27  protected $cache;
28 
35  $this->lb = $lb;
36  $this->cache = $cache;
37  }
38 
43  public function findByUser( User $user ) {
44  $oathUser = $this->cache->get( $user->getName() );
45  if ( !$oathUser ) {
46  $oathUser = new OATHUser( $user, null );
47 
48  $uid = CentralIdLookup::factory()->centralIdFromLocalUser( $user );
49  $res = $this->getDB( DB_REPLICA )->selectRow(
50  'oathauth_users',
51  '*',
52  [ 'id' => $uid ],
53  __METHOD__
54  );
55  if ( $res ) {
56  $key = new OATHAuthKey( $res->secret, explode( ',', $res->scratch_tokens ) );
57  $oathUser->setKey( $key );
58  }
59 
60  $this->cache->set( $user->getName(), $oathUser );
61  }
62  return $oathUser;
63  }
64 
68  public function persist( OATHUser $user ) {
69  $this->getDB( DB_MASTER )->replace(
70  'oathauth_users',
71  [ 'id' ],
72  [
73  'id' => CentralIdLookup::factory()->centralIdFromLocalUser( $user->getUser() ),
74  'secret' => $user->getKey()->getSecret(),
75  'scratch_tokens' => implode( ',', $user->getKey()->getScratchTokens() ),
76  ],
77  __METHOD__
78  );
79  $this->cache->set( $user->getUser()->getName(), $user );
80  }
81 
85  public function remove( OATHUser $user ) {
86  $this->getDB( DB_MASTER )->delete(
87  'oathauth_users',
88  [ 'id' => CentralIdLookup::factory()->centralIdFromLocalUser( $user->getUser() ) ],
89  __METHOD__
90  );
91  $this->cache->delete( $user->getUser()->getName() );
92  }
93 
98  private function getDB( $index ) {
99  global $wgOATHAuthDatabase;
100 
101  return $this->lb->getConnectionRef( $index, [], $wgOATHAuthDatabase );
102  }
103 }
OATHUserRepository\$cache
BagOStuff $cache
Definition: OATHUserRepository.php:27
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:244
OATHUserRepository\persist
persist(OATHUser $user)
Definition: OATHUserRepository.php:68
OATHUserRepository
Definition: OATHUserRepository.php:22
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
BagOStuff
interface is intended to be more or less compatible with the PHP memcached client.
Definition: BagOStuff.php:47
$res
$res
Definition: database.txt:21
cache
you have access to all of the normal MediaWiki so you can get a DB use the cache
Definition: maintenance.txt:52
OATHUserRepository\getDB
getDB( $index)
Definition: OATHUserRepository.php:98
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
OATHAuthKey
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
Definition: OATHAuthKey.php:26
OATHUserRepository\$lb
LoadBalancer $lb
Definition: OATHUserRepository.php:24
OATHUser
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
Definition: OATHUser.php:24
OATHUserRepository\findByUser
findByUser(User $user)
Definition: OATHUserRepository.php:43
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
OATHUser\getUser
getUser()
Definition: OATHUser.php:44
OATHUserRepository\__construct
__construct(LoadBalancer $lb, BagOStuff $cache)
OATHUserRepository constructor.
Definition: OATHUserRepository.php:34
Wikimedia\Rdbms\LoadBalancer
Database connection, tracking, load balancing, and transaction manager for a cluster.
Definition: LoadBalancer.php:40
Wikimedia\Rdbms\DBConnRef
Helper class to handle automatically marking connections as reusable (via RAII pattern) as well handl...
Definition: DBConnRef.php:15
CentralIdLookup\factory
static factory( $providerId=null)
Fetch a CentralIdLookup.
Definition: CentralIdLookup.php:46
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:53
User\getName
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2394