Elgg  Version 4.3
UsersTable.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
5 use Elgg\Config;
6 use Elgg\Database;
9 
16 class UsersTable {
17 
18  use TimeUsing;
19 
23  protected $config;
24 
28  protected $db;
29 
33  protected $metadata;
34 
43  $this->config = $config;
44  $this->db = $db;
45  $this->metadata = $metadata;
46  }
47 
55  public function getByUsername($username) {
56  if (empty($username)) {
57  return false;
58  }
59 
60  // Fixes #6052. Username is frequently sniffed from the path info, which,
61  // unlike $_GET, is not URL decoded. If the username was not URL encoded,
62  // this is harmless.
63  $username = rawurldecode($username);
64  if (empty($username)) {
65  return false;
66  }
67 
68  $logged_in_user = elgg_get_logged_in_user_entity();
69  if (!empty($logged_in_user) && ($logged_in_user->username === $username)) {
70  return $logged_in_user;
71  }
72 
74  'types' => 'user',
75  'metadata_name_value_pairs' => [
76  [
77  'name' => 'username',
78  'value' => $username,
79  'case_sensitive' => false,
80  ],
81  ],
82  'limit' => 1,
83  ]);
84 
85  return $users ? $users[0] : false;
86  }
87 
94  public function getByEmail($email) {
95  if (!$email) {
96  return [];
97  }
98 
100  'types' => 'user',
101  'metadata_name_value_pairs' => [
102  [
103  'name' => 'email',
104  'value' => $email,
105  'case_sensitive' => false,
106  ],
107  ],
108  'limit' => 1,
109  ]);
110 
111  return $users ? : [];
112  }
113 
126  public function findActive(array $options = []) {
127 
128  $options = array_merge([
129  'seconds' => 600,
130  'limit' => $this->config->default_limit,
131  'offset' => 0,
132  ], $options);
133 
134  // cast options we're sending to hook
135  foreach (['seconds', 'limit', 'offset'] as $key) {
136  $options[$key] = (int) $options[$key];
137  }
138  $options['count'] = (bool) $options['count'];
139 
140  // allow plugins to override
141  $params = [
142  'seconds' => $options['seconds'],
143  'limit' => $options['limit'],
144  'offset' => $options['offset'],
145  'count' => $options['count'],
146  'options' => $options,
147  ];
148  $data = _elgg_services()->hooks->triggerDeprecated('find_active_users', 'system', $params, null, "No longer use the 'find_active_users', 'system' hook", '4.3');
149  // check null because the handler could legitimately return falsey values.
150  if ($data !== null) {
151  return $data;
152  }
153 
154  $time = $this->getCurrentTime()->getTimestamp() - $options['seconds'];
155  return elgg_get_entities([
156  'type' => 'user',
157  'limit' => $options['limit'],
158  'offset' => $options['offset'],
159  'count' => $options['count'],
160  'wheres' => function(QueryBuilder $qb, $main_alias) use ($time) {
161  return $qb->compare("{$main_alias}.last_action", '>=', $time, ELGG_VALUE_INTEGER);
162  },
163  'order_by' => new OrderByClause('e.last_action', 'DESC'),
164  ]);
165  }
166 
175  public function generateInviteCode(string $username): string {
176  $time = $this->getCurrentTime()->getTimestamp();
177  $token = _elgg_services()->hmac->getHmac([$time, $username])->getToken();
178 
179  return "{$time}.{$token}";
180  }
181 
191  public function validateInviteCode(string $username, string $code): bool {
192  // validate the format of the token created by self::generateInviteCode()
193  $matches = [];
194  if (!preg_match('~^(\d+)\.([a-zA-Z0-9\-_]+)$~', $code, $matches)) {
195  return false;
196  }
197  $time = (int) $matches[1];
198  $mac = $matches[2];
199 
200  return _elgg_services()->hmac->getHmac([$time, $username])->matchesToken($mac);
201  }
202 }
$params
Saves global plugin settings.
Definition: save.php:13
if(empty($user_guids)) $users
Definition: ban.php:12
__construct(Config $config, Database $db, MetadataTable $metadata)
Constructor.
Definition: UsersTable.php:42
The Elgg database.
Definition: Database.php:25
findActive(array $options=[])
Return users (or the number of them) who have been active within a recent period. ...
Definition: UsersTable.php:126
const ELGG_VALUE_INTEGER
Value types.
Definition: constants.php:126
if(!$annotation instanceof ElggAnnotation) $time
Definition: time.php:20
$email
Definition: change_email.php:7
if(elgg_trigger_plugin_hook('usersettings:save', 'user', $hooks_params, true)) foreach($request->validation() ->all() as $item) $data
Definition: save.php:53
Database abstraction query builder.
$username
Definition: delete.php:23
trait TimeUsing
Adds methods for setting the current time (for testing)
Definition: TimeUsing.php:10
getByEmail($email)
Get an array of users from an email address.
Definition: UsersTable.php:94
getByUsername($username)
Get user by username.
Definition: UsersTable.php:55
$options
Elgg admin footer.
Definition: footer.php:6
getCurrentTime($modifier= '')
Get the (cloned) time.
Definition: TimeUsing.php:25
validateInviteCode(string $username, string $code)
Validate a user&#39;s invite code.
Definition: UsersTable.php:191
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:545
generateInviteCode(string $username)
Generates a unique invite code for a user.
Definition: UsersTable.php:175
compare($x, $comparison, $y=null, $type=null, $case_sensitive=null)
Build value comparison clause.
$token
Extends QueryBuilder with ORDER BY clauses.
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
Users helper service.
Definition: UsersTable.php:16
_elgg_services()
Get the global service provider.
Definition: elgglib.php:638
elgg_get_logged_in_user_entity()
Return the current logged in user, or null if no user is logged in.
Definition: sessions.php:24
$qb
Definition: queue.php:11
This class interfaces with the database to perform CRUD operations on metadata.