Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [Laravel] Czy użytkownik jest online?
Forum PHP.pl > Forum > PHP
markuz
Witam,

Na podstawie http://laravel.io/forum/03-03-2014-sentry-3-users-online
utworzyłem model Online oraz tabelę sessions.

Chciałbym dodać metodę która sprawdzi czy użytkownik jest zalogowany.

np.

  1. $user = User::find(1);
  2. if($user->isOnline())
  3. echo "Zalogowany";


Moje doświadczenie w Laravel jest bardzo niskie. Macie może jakieś porady jak się za to zabrać?

# models/Online.php

  1. <?php
  2.  
  3. use Illuminate\Database\Eloquent\Model;
  4.  
  5. //use Session;
  6.  
  7. class Online extends Model {
  8.  
  9. /**
  10.   * {@inheritDoc}
  11.   */
  12. public $table = 'sessions';
  13.  
  14. /**
  15.   * {@inheritDoc}
  16.   */
  17. public $timestamps = false;
  18.  
  19. /**
  20.   * Returns all the guest users.
  21.   *
  22.   * @param \Illuminate\Database\Eloquent\Builder $query
  23.   * @return \Illuminate\Database\Eloquent\Builder
  24.   */
  25. public function scopeGuests($query)
  26. {
  27. return $query->whereNull('user_id');
  28. }
  29.  
  30. /**
  31.   * Returns all the registered users.
  32.   *
  33.   * @param \Illuminate\Database\Eloquent\Builder $query
  34.   * @return \Illuminate\Database\Eloquent\Builder
  35.   */
  36. public function scopeRegistered($query)
  37. {
  38. return $query->whereNotNull('user_id')->with('user');
  39. }
  40.  
  41. /**
  42.   * Updates the session of the current user.
  43.   *
  44.   * @param \Illuminate\Database\Eloquent\Builder $query
  45.   * @return \Illuminate\Database\Eloquent\Builder
  46.   */
  47. public function scopeUpdateCurrent($query)
  48. {
  49.  
  50. return $query->where('id', Session::getId())->update(array(
  51. 'user_id' => Auth::user() ? Auth::user()->id : null
  52. ));
  53. }
  54.  
  55.  
  56. /**
  57.   * Returns the user that belongs to this entry.
  58.   *
  59.   * @return \Cartalyst\Sentry\Users\EloquentUser
  60.   */
  61. public function user()
  62. {
  63. return $this->belongsTo('User'); # Sentry 3
  64. // return $this->belongsTo('Cartalyst\Sentry\Users\Eloquent\User'); # Sentry 2
  65. }
  66.  
  67. }


#models/User

  1. <?php
  2.  
  3. use Illuminate\Auth\UserTrait;
  4. use Illuminate\Auth\UserInterface;
  5. use Illuminate\Auth\Reminders\RemindableTrait;
  6. use Illuminate\Auth\Reminders\RemindableInterface;
  7.  
  8. class User extends Eloquent implements UserInterface, RemindableInterface {
  9.  
  10. use UserTrait, RemindableTrait;
  11.  
  12. /**
  13. * The database table used by the model.
  14. *
  15. * @var string
  16. */
  17. protected $table = 'users';
  18.  
  19. /**
  20. * The attributes excluded from the model's JSON form.
  21. *
  22. * @var array
  23. */
  24. protected $hidden = array('password');
  25.  
  26. }



Update

Do modelu User dodałem metode isOnline:

  1. public function isOnline()
  2. {
  3. return Online::registered()->where('user_id', $this->getAuthIdentifier())->count();
  4. }


i działa tak jak chciałem - jednak nie wiem czy jest to najlepsze rozwiązanie - gdyby ktoś miał coś lepszego jestem otwarty na propozycje smile.gif


markuz
@memory - nie widzę związku z tematem.. Używam wymienionej przez Ciebie autoryzacji jednak funkcja czy użytkownik jest zalogowany nie jest w nią wbudowana (chyba).
memory
Faktycznie źle cie zrozumiałem
Pyton_000
Ustalasz czas który będzie traktowany że user jest online np. 5 min od ostatniej aktywności.
Przy każdym przechodzeniu przez stronę robisz update rekordu w session np. pole last_activity.

I sprawdzasz czy Session::where('user_id', 1)->where('last_activity' < time()+300)->get();

Jeżeli zwróci wynik to jest, jak nie to nie.
Przy wylogowaniu możesz czyścić tabelę session z wpisów dla tego usera. Koniec.
ctom
@Pyton_000 on zrobił tabele sessions i model Online (dla niej)
a to :
Kod
Session::where('user_id', 1)->where('last_activity' < time()+300)->get();
to się chyba gryzie z fasadą sesji.

@markuz linku, który podałeś mam akapit "How to Use" - "pobaw się tym" i zobacz co zwraca konkretne wywołanie
markuz
Spoko już sobie poradziłem - ale doszedłem do wniosku, że status online/offline muszę zrobić przez sockety i w ten sposób porzuciłem model Online smile.gif
Pyton_000
*ctom to taki ogólny pomysł wink.gif Trzeba dawać wędkę a nie rybkę wink.gif

@up sockety? sciana.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.