Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Laravel] Czy użytkownik jest online?
markuz
post 21.12.2014, 16:57:45
Post #1





Grupa: Zarejestrowani
Postów: 1 240
Pomógł: 278
Dołączył: 11.03.2008

Ostrzeżenie: (0%)
-----


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




Ten post edytował markuz 21.12.2014, 17:08:59


--------------------
Go to the top of the page
+Quote Post
memory
post 21.12.2014, 17:49:12
Post #2





Grupa: Zarejestrowani
Postów: 616
Pomógł: 84
Dołączył: 29.11.2006
Skąd: bełchatów

Ostrzeżenie: (0%)
-----


http://laravel.com/docs/4.2/security#authenticating-users
Go to the top of the page
+Quote Post
markuz
post 21.12.2014, 18:06:14
Post #3





Grupa: Zarejestrowani
Postów: 1 240
Pomógł: 278
Dołączył: 11.03.2008

Ostrzeżenie: (0%)
-----


@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).


--------------------
Go to the top of the page
+Quote Post
memory
post 21.12.2014, 18:56:26
Post #4





Grupa: Zarejestrowani
Postów: 616
Pomógł: 84
Dołączył: 29.11.2006
Skąd: bełchatów

Ostrzeżenie: (0%)
-----


Faktycznie źle cie zrozumiałem
Go to the top of the page
+Quote Post
Pyton_000
post 21.12.2014, 19:14:04
Post #5





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


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.
Go to the top of the page
+Quote Post
ctom
post 21.12.2014, 20:32:54
Post #6





Grupa: Zarejestrowani
Postów: 321
Pomógł: 55
Dołączył: 19.04.2009

Ostrzeżenie: (0%)
-----


@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


--------------------
Polecam MyDevil hosting idealny dla deweloperów
Go to the top of the page
+Quote Post
markuz
post 21.12.2014, 20:56:28
Post #7





Grupa: Zarejestrowani
Postów: 1 240
Pomógł: 278
Dołączył: 11.03.2008

Ostrzeżenie: (0%)
-----


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


--------------------
Go to the top of the page
+Quote Post
Pyton_000
post 22.12.2014, 09:45:31
Post #8





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


*ctom to taki ogólny pomysł wink.gif Trzeba dawać wędkę a nie rybkę wink.gif

@up sockety? sciana.gif
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 19.07.2025 - 19:18