Elgg  Version 3.0
UsersTable.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
5 use Elgg\Config as Conf;
6 use Elgg\Database;
9 use ElggUser;
10 use Exception;
12 
22 class UsersTable {
23 
25 
29  protected $config;
30 
34  protected $db;
35 
39  protected $metadata;
40 
49  $this->config = $config;
50  $this->db = $db;
51  $this->metadata = $metadata;
52  }
53 
61  public function getByUsername($username) {
62 
63  // Fixes #6052. Username is frequently sniffed from the path info, which,
64  // unlike $_GET, is not URL decoded. If the username was not URL encoded,
65  // this is harmless.
66  $username = rawurldecode($username);
67 
68  if (!$username) {
69  return false;
70  }
71 
72  $entity =_elgg_services()->dataCache->usernames->load($username);
73  if ($entity instanceof ElggUser) {
74  return $entity;
75  }
76 
77  $users = elgg_get_entities([
78  'types' => 'user',
79  'metadata_name_value_pairs' => [
80  [
81  'name' => 'username',
82  'value' => $username,
83  'case_sensitive' => false,
84  ],
85  ],
86  'limit' => 1,
87  ]);
88 
89  return $users ? $users[0] : false;
90  }
91 
98  public function getByEmail($email) {
99  if (!$email) {
100  return [];
101  }
102 
103  $users = elgg_get_entities([
104  'types' => 'user',
105  'metadata_name_value_pairs' => [
106  [
107  'name' => 'email',
108  'value' => $email,
109  'case_sensitive' => false,
110  ],
111  ],
112  'limit' => 1,
113  ]);
114 
115  return $users ? : [];
116  }
117 
130  public function findActive(array $options = []) {
131 
132  $options = array_merge([
133  'seconds' => 600,
134  'limit' => $this->config->default_limit,
135  ], $options);
136 
137  // cast options we're sending to hook
138  foreach (['seconds', 'limit', 'offset'] as $key) {
139  $options[$key] = (int) $options[$key];
140  }
141  $options['count'] = (bool) $options['count'];
142 
143  // allow plugins to override
144  $params = [
145  'seconds' => $options['seconds'],
146  'limit' => $options['limit'],
147  'offset' => $options['offset'],
148  'count' => $options['count'],
149  'options' => $options,
150  ];
151  $data = _elgg_services()->hooks->trigger('find_active_users', 'system', $params, null);
152  // check null because the handler could legitimately return falsey values.
153  if ($data !== null) {
154  return $data;
155  }
156 
157  $time = $this->getCurrentTime()->getTimestamp() - $options['seconds'];
158  return elgg_get_entities([
159  'type' => 'user',
160  'limit' => $options['limit'],
161  'offset' => $options['offset'],
162  'count' => $options['count'],
163  'wheres' => function(QueryBuilder $qb, $main_alias) use ($time) {
164  return $qb->compare("{$main_alias}.last_action", '>=', $time, ELGG_VALUE_INTEGER);
165  },
166  'order_by' => new OrderByClause('e.last_action', 'DESC'),
167  ]);
168  }
169 
185  public function register($username, $password, $name, $email, $allow_multiple_emails = false, $subtype = null) {
186  _elgg_services()->accounts->register($username, $password, $name, $email, $allow_multiple_emails, $subtype);
187  }
188 
197  public function generateInviteCode($username) {
198  $time = $this->getCurrentTime()->getTimestamp();
199  return "$time." . _elgg_services()->hmac->getHmac([(int) $time, $username])->getToken();
200  }
201 
211  public function validateInviteCode($username, $code) {
212  // validate the format of the token created by ->generateInviteCode()
213  if (!preg_match('~^(\d+)\.([a-zA-Z0-9\-_]+)$~', $code, $m)) {
214  return false;
215  }
216  $time = $m[1];
217  $mac = $m[2];
218 
219  return _elgg_services()->hmac->getHmac([(int) $time, $username])->matchesToken($mac);
220  }
221 }
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
$username
Definition: delete.php:23
$params
Saves global plugin settings.
Definition: save.php:13
findActive(array $options=[])
Return users (or the number of them) who have been active within a recent period. ...
Definition: UsersTable.php:130
const ELGG_VALUE_INTEGER
Value types.
Definition: constants.php:138
$subtype
Definition: delete.php:22
if(elgg_trigger_plugin_hook('usersettings:save', 'user', $hooks_params, true)) foreach($request->validation() ->all() as $item) $data
Definition: save.php:57
Database abstraction query builder.
getCurrentTime($modifier= '')
Get the (cloned) time.
Definition: TimeUsing.php:27
$email
Definition: register.php:18
getByEmail($email)
Get an array of users from an email address.
Definition: UsersTable.php:98
getByUsername($username)
Get user by username.
Definition: UsersTable.php:61
$options
Elgg admin footer.
Definition: footer.php:6
$code
if(!$user||!$user->canEdit()) $password
$entity
Definition: reset.php:8
__construct(Conf $config, Database $db, MetadataTable $metadata)
Constructor.
Definition: UsersTable.php:48
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:545
if(!$entity instanceof ElggEntity) $time
Definition: time.php:21
compare($x, $comparison, $y=null, $type=null, $case_sensitive=null)
Build value comparison clause.
elgg ElggUser
Definition: ElggUser.js:12
validateInviteCode($username, $code)
Validate a user&#39;s invite code.
Definition: UsersTable.php:211
Extends QueryBuilder with ORDER BY clauses.
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:55
WARNING: API IN FLUX.
Definition: UsersTable.php:22
generateInviteCode($username)
Generates a unique invite code for a user.
Definition: UsersTable.php:197
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
trait TimeUsing
Adds methods for setting the current time (for testing)
Definition: TimeUsing.php:12
This class interfaces with the database to perform CRUD operations on metadata.