Skip to content

Instantly share code, notes, and snippets.

@Ardakilic
Last active January 25, 2016 16:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Ardakilic/6352909 to your computer and use it in GitHub Desktop.
Save Ardakilic/6352909 to your computer and use it in GitHub Desktop.
To find all unbanned/unsuspended users with Sentry 2 using Laravel 4, try this method:
<?php
class User extends Cartalyst\Sentry\Users\Eloquent\User {
/**
* @author Arda Kılıçdağı,
* @param $orderBy string how to order
* @param $sortBy string Sort like Ascending or Descending, values are asc/desc
* @param $toArray boolean whether to return the output as array or not. Default is false
*
* Access it like: User::activeUsers();
* To alter sorting, e.g: to get latest users first: User::activeUsers('user_id', 'desc');
* To get output like array, pass the 3rd parameter as true: User::activeUsers(null, null, true);
**/
public static function activeUsers($orderBy='first_name', $sortBy='asc', $toArray=false) {
$out = Sentry::getUserProvider()->createModel()
->join('throttle', 'throttle.user_id', '=', 'users.id', 'left')
->where(function($q){
$q->where('throttle.banned', 0);
$q->orWhere('throttle.banned', null);
})
->where(function($q){
$q->where('throttle.suspended', 0);
$q->orWhere('throttle.suspended', null);
})
->groupBy('users.id')
->orderBy('users.'.$orderBy, $sortBy)
->select('users.*')
->get();
if($toArray) {
$out = $out->toArray();
}
return $out;
}
}
@Ardakilic
Copy link
Author

Updated the method a bit. I've put it in a User model which extends Sentry 2 Eloquent User model. You can change your app/models/User.php with this if you want to use Sentry 2 instead Laravel 4's auth methods.

@macliems
Copy link

Nice! But what if I want to perform a search (username LIKE seachTerm) to get a subset of the active users? I tried where clauses everywhere, but to no avail. Can you help me?

@Ardakilic
Copy link
Author

@macliems - Have you tried using something like where('users.first_name', 'LIKE', '%pattern%'); ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment