How to display all connected users in my home?
This is a very basic, but hopefully an efective way to detect both Guest and Registered users on your Laravel5 application.
Step 1
Open the file config/session.php and change the driver to database.
Step 2
We need to create the sessions table, so use the following artisan command php artisan session:table to generate the migration file.
Step 3
On this newly generated migration, you need to add a new user_id column, this is so we can relate the session to a user, if that user is logged in of course.
Open the file migrations/xxxx_xx_xx_xxxxxx_create_session_table.php and add the following inside the Schema::create:
$t->integer('user_id')->nullable();
Here is how the full migration should look:
<?php
use Illuminate\Database\Migrations\Migration;
class CreateSessionTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('sessions', function($t)
{
$t->string('id')->unique();
$t->text('payload');
$t->integer('last_activity');
$t->integer('user_id')->nullable();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('sessions');
}
}
Step 4
Run composer dump-autoload and php artisan migrate.
Note: If you don't have Composer installed globally, just use php composer.phar dump-autoload.
Step 5
Save the Eloquent Model somewhere on your application as Session.php.
Note: The recommended place to save this is on the app directory.
Step 6
Now you just need to know how to use it.
.
.
.
Usage
Place the following Session::updateCurrent(); somewhere on your code, as this will make sure that the session entry for the current user get's updated, just an example, you can place it on your app/routes.php file.
Get all users (Guests + Registered)
$all = Session::all();
If you need to check all users online for a certain period, like 10 minutes, you need to call the activity(:limit) method, like so:
$all = Session::activity(10)->get();
Note: This method can be used in combination with the guests() and/or registered() methods.
Guest Users
Grab all
$guests = Session::guests()->get();
Get the # of Guest users
$total = Session::guests()->count();
Registered Users
Grab all
$registered = Session::registered()->get();
foreach ($registered as $online) {
// You can retrieve the user information using something like:
var_dump($online->user->email);
}
Get the # of Registered users
$total = Session::registered()->count();
Eloquent model:
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Session;
use Illuminate\Database\Eloquent\Builder;
use Cartalyst\Sentinel\Laravel\Facades\Sentinel;
class Session extends Model
{
/**
* {#inheritdoc}
*/
public $table = 'sessions';
/**
* {#inheritdoc}
*/
public $timestamps = false;
/**
* Returns the user that belongs to this entry.
*
* #return \Cartalyst\Sentinel\Users\EloquentUser
*/
public function user()
{
return $this->belongsTo('Cartalyst\Sentinel\Users\EloquentUser');
}
/**
* Returns all the users within the given activity.
*
* #param \Illuminate\Database\Eloquent\Builder $query
* #param int $limit
* #return \Illuminate\Database\Eloquent\Builder
*/
public function scopeActivity($query, $limit = 10)
{
$lastActivity = strtotime(Carbon::now()->subMinutes($limit));
return $query->where('last_activity', '>=', $lastActivity);
}
/**
* Returns all the guest users.
*
* #param \Illuminate\Database\Eloquent\Builder $query
* #return \Illuminate\Database\Eloquent\Builder
*/
public function scopeGuests(Builder $query)
{
return $query->whereNull('user_id');
}
/**
* Returns all the registered users.
*
* #param \Illuminate\Database\Eloquent\Builder $query
* #return \Illuminate\Database\Eloquent\Builder
*/
public function scopeRegistered(Builder $query)
{
return $query->whereNotNull('user_id')->with('user');
}
/**
* Updates the session of the current user.
*
* #param \Illuminate\Database\Eloquent\Builder $query
* #return \Illuminate\Database\Eloquent\Builder
*/
public function scopeUpdateCurrent(Builder $query)
{
$user = Sentinel::check();
return $query->where('id', Session::getId())->update([
'user_id' => $user ? $user->id : null
]);
}
}
Alternatively you can try this.
Related
I am new to Laravel 8, I am trying to make a multi auth system for admin and user.
I made a guard for admin, in FortifyServiceProvider in register method I added return Auth::guard('admin');
These are routes
Route::group(['prefix'=> 'admin', 'middleware'=>['admin:admin']], function(){
Route::get('/login', [AdminController::class, 'loginForm']);
Route::post('/login',[AdminController::class, 'storeAdmin'])->name('admin.login');
});
Route::middleware(['auth:sanctum,admin', 'verified'])->get('/admin/dashboard', function () {
return view('dashboard');
})->name('dashboard');
Route::middleware(['auth:sanctum,web', 'verified'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
AdminController:
<?php
namespace App\Http\Controllers;
use Illuminate\Contracts\Auth\StatefulGuard;
use Laravel\Fortify\Http\Controllers\AuthenticatedSessionController;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Routing\Pipeline;
use App\Actions\Fortify\AttemptToAuthenticate;
use App\Actions\Fortify\RedirectIfTwoFactorAuthenticatable;
use Laravel\Fortify\Actions\EnsureLoginIsNotThrottled;
use Laravel\Fortify\Actions\PrepareAuthenticatedSession;
use App\Http\Responses\LoginResponse;
use Laravel\Fortify\Contracts\LoginViewResponse;
use Laravel\Fortify\Contracts\LogoutResponse;
use Laravel\Fortify\Features;
use Laravel\Fortify\Fortify;
use Laravel\Fortify\Http\Requests\LoginRequest;
class AdminController extends Controller
{
/**
* The guard implementation.
*
* #var \Illuminate\Contracts\Auth\StatefulGuard
*/
protected $guard;
/**
* Create a new controller instance.
*
* #param \Illuminate\Contracts\Auth\StatefulGuard $guard
* #return void
*/
public function __construct(StatefulGuard $guard)
{
$this->guard = $guard;
}
public function loginForm(){
return view('auth.login', ['guard' => 'admin']);
}
/**
* Show the login view.
*
* #param \Illuminate\Http\Request $request
* #return \Laravel\Fortify\Contracts\LoginViewResponse
*/
public function create(Request $request): LoginViewResponse
{
return app(LoginViewResponse::class);
}
/**
* Attempt to authenticate a new session.
*
* #param \Laravel\Fortify\Http\Requests\LoginRequest $request
* #return mixed
*/
public function storeAdmin(LoginRequest $request)
{
return $this->loginPipeline($request)->then(function ($request) {
return app(LoginResponse::class);
});
}
/**
* Get the authentication pipeline instance.
*
* #param \Laravel\Fortify\Http\Requests\LoginRequest $request
* #return \Illuminate\Pipeline\Pipeline
*/
protected function loginPipeline(LoginRequest $request)
{
if (Fortify::$authenticateThroughCallback) {
return (new Pipeline(app()))->send($request)->through(array_filter(
call_user_func(Fortify::$authenticateThroughCallback, $request)
));
}
if (is_array(config('fortify.pipelines.login'))) {
return (new Pipeline(app()))->send($request)->through(array_filter(
config('fortify.pipelines.login')
));
}
return (new Pipeline(app()))->send($request)->through(array_filter([
config('fortify.limiters.login') ? null : EnsureLoginIsNotThrottled::class,
Features::enabled(Features::twoFactorAuthentication()) ? RedirectIfTwoFactorAuthenticatable::class : null,
AttemptToAuthenticate::class,
PrepareAuthenticatedSession::class,
]));
}
/**
* Destroy an authenticated session.
*
* #param \Illuminate\Http\Request $request
* #return \Laravel\Fortify\Contracts\LogoutResponse
*/
public function destroy(Request $request): LogoutResponse
{
$this->guard->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return app(LogoutResponse::class);
}
}
I can log in as an admin but got the error when try to log in as a user
thanks for help
Because bootstrap cache is in bootstrap/cache/routes_v7.php.
Solution: Remove cache
php artisan cache:clear
php artisan optimize
Reload.
DONE
I'm a little confused about the management of mail notifications failed from a queue.
I've created a mail notification class that I use to send a same notification to multiple users.
The process works well, but i'm trying to set up a management for the notifications that would fail (like sending a mail to the admin users to alert them about the failed notifications).
Here is the mail notification class :
class MyCustomMailNotification extends Notification implements
ShouldQueue {
use Queueable;
/**
* The number of times the job may be attempted.
*
* #var int
*/
public $tries = 3;
/**
* The number of seconds the job can run before timing out.
*
* #var int
*/
//public $timeout = 90;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* #param mixed $notifiable
* #return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject('My Subject')
->greeting('My greeting')
->line('My mail body')
->salutation('My salutations');
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
//
];
}
public function failed(Exception $e)
{
dd('Entered failed from MyCustomMailNotification : ' . $e));
}
}
I've set a listener "LogNotification" To reach the handle of notification event, with a specific instruction to generate a fail :
EventServiceProvider:
/**
* The event listener mappings for the application.
*
* #var array
*/
protected $listen = [
'Illuminate\Notifications\Events\NotificationSent' => [
'App\Listeners\LogNotification',
],
];
Listener:
namespace App\Listeners;
use Illuminate\Notifications\Events\NotificationSent; use
Illuminate\Queue\InteractsWithQueue; use
Illuminate\Contracts\Queue\ShouldQueue;
class LogNotification {
/**
* Create the event listener.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* #param NotificationSent $event
* #return void
*/
public function handle(NotificationSent $event)
{
$result = 1/0;
}
}
The mailing is made in a controller like this:
$when = Carbon::now()->addSeconds(5);
foreach ($users as $user) {
$user->notify((new MyCustomMailNotification())->delay($when));
}
As in the failed function, I don't get any information of the notification that has failed, my question is:
How can I related a failure with the notification that has failed?
The goal is to be able to get the information of the user who has therefore not received his notification email.
Thanks for any help, idea or solution!
Well, that was so simple that I didn't see it...
As the failed function is inside the notification class, I can related to the notification simply with:
$this->id
And then of course get all the informations about the notification from model/table like user id (notifiable_id) and data field with custom informations.
I'm a begginer in Symfony 4 and I'm developing an API Rest. I want to create a PUT resource that can handle many update cases.
In my case, I have a user with many properties but I will take an example with 3 properties to keep things simple :
User {
username,
email,
password
}
My PUT resource can be called in order to update Username, update Email or update Password. For example, to update Username, the user of my API will send a PUT request with only username :
{
username: "New username"
}
Same for email and password, he will only send the property he wants to change.
My problem is in my Controller, I have to do things like this :
/**
* #Rest\Put("/user/{id}")
* #param Request $request
* #return View
*/
public function putUserRequest(Request $request)
{
$userId = $request->get('id');
$user = $this->doctrine->getRepository(User::class)->findOneBy('id' => $userId);
$userFromRequest = $this->serializer->deserialize($request->getContent(), User::class, 'json');
if ($userFromRequest->getUsername() != NULL) {
$user->setUsername($userFromRequest->getUsername())
}
if ($userFromRequest->getEmail() != NULL) {
$user->setEmail($userFromRequest->getEmail())
}
if ($userFromRequest->getPassword() != NULL) {
$user->setPassword($userFromRequest->getPassword())
}
// ...
}
In my example I have only 3 properties, but imagine when I have 30 properties.
With Symfony 3 I used forms to validate / save my datas :
$form->submit($request->request->all(), $clearMissing);
Where $clearMissing is false to keep datas not provided by the user of my API. I can't find a way to do it with serializer but I guess I'm doing things wrong.
Thanks.
If I understand correctly, You can use the validator Component like this :
/**
* #Rest\Put("/user/{id}")
* #param Request $request
* #return View
*/
public function putUserRequest(User $user, Request $request, ValidatorInterface $validator)
{
$data = $request->getContent();
$this->serializer->deserialize($data, User::class, 'json', ['object_to_populate' => $user]);
//At this moment, the data you sent is merged with your user entity
/** #var ConstraintViolationList $errors */
$errors = $validator->validate($user, null ,['groups' => 'user_update']);
if (count($errors) > 0) {
//return json reponse with formated errors;
}
//if ok $entityManager->flush() and Response Json with serialization group;
...
}
In your user class :
class User implements UserInterface
{
/**
* #Assert\Email(groups={"user_create", "user_update"})
*/
private $email;
/**
* #Assert\NotBlank(groups={"user_create", "user_update"})
* #Assert\Length(min=7, groups={"user_create", "user_update"})
*/
private $password;
/**
* #Assert\Length(min=2, groups={"user_create", "user_update"} )
*/
private $username;
}
Related Validator component documentation : https://symfony.com/doc/current/validation/groups.html
You can also check this project : https://github.com/attineos/relation-deserializer
FYI : I'm very new to Laravel and doing my best to learn it properly.
Working on an auth driver that uses a soap service to authenticate.
Error I get when trying to test with Auth::attempt()
Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_COMPILE_ERROR)
Declaration of Project\Providers\AuthUserProvider::retrieveByToken() must be compatible with Illuminate\Auth\UserProviderInterface::retrieveByToken($identifier, $token)
Here is the driver...
<?php namespace Project\Providers;
use Illuminate\Auth\UserProviderInterface;
use Illuminate\Auth\GenericUser;
use Illuminate\Auth\UserInterface;
class AuthUserProvider implements UserProviderInterface {
/**
* External webservice for authentication
*/
private $webservice;
/**
* The user object.
*/
private $user;
/**
* Constructor
*
* #return void
*/
public function __construct(\Project\Webservice\AuthCheckApi $webservice)
{
$this->webservice = $webservice;
$this->user = null;
}
/**
* Retrieves a user by id
*
* #param int $identifier
* #return mixed null|array
*/
public function retrieveByID($identifier)
{
$this->user = is_null($this->user) ? $this->webservice->find($identifier) : $this->user;
return $this->user;
}
/**
* Tries to find a user based on the credentials passed.
*
* #param array $crendtials username|password
* #return mixed bool|UserInterface
*/
public function retrieveByCredentials(array $credentials)
{
if(!$user = $this->webservice->byusername($credentials['username'],$credentials['password'])) return false;
return new GenericUser($user);
}
/**
* Validates the credentials passed to the ones in webservice.
*
* #param UserInterface $user
* #param array $credentials
* #return bool
*/
public function validateCredentials(\Illuminate\Auth\UserInterface $user, array $credentials)
{
$validated = $this->webservice->validateCredentials($user,$credentials['username']);
return true;
}
/**
* Needed by Laravel 4.1.26 and above
*/
public function retrieveByToken()
{
return true;
}
/**
* Needed by Laravel 4.1.26 and above
*/
public function updateRememberToken()
{
return false;
}
}
Thanks for any help.
You are implementing the UserProviderInterface so you need to add the complete definition of all functions of the interface, here you are forgetting the arguments for the last two function
public function retrieveByToken($identifier, $token)
{
}
public function updateRememberToken($user, $token)
{
}
I want to create a RESTful API in Laravel 4 to use in my BackboneJS apps. What is the best way for doing this? Does the Laravel 4 framework provides a good solution for this.
This is an example for creating an API that stores bookmarks. It uses the Route::resource() method.
Creating a RESTful controller in Laravel 4
POST = store() (Create a new entry)
DELETE = destroy($id) (Delete an entry)
GET = index() (Get all entries)
GET = show($id) (Get one entry)
PUT = update($id) (Update an entry)
The best extension for testing your API's:
Chrome extension Postman REST client
This is my simple router and controller, I did the same kind of project. You might want to try Postman RESTful client for Chrome to test your API,
routes.php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/
// Route group for API versioning
Route::group(array('prefix' => 'api/v1'), function() {
Route::resource('bookmarks', 'BookmarkController',
array('except' => array('create', 'edit')));
});
BookmarkController.php
class BookmarkController extends Controller {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index() {
return Bookmark::all();
}
/**
* Store a newly created resource in storage.
*
* #return Response
*/
public function store() {
$bookmark = new Bookmark;
$bookmark->url = Input::get('url');
$bookmark->description = Input::get('description');
$bookmark->tags = Input::get('tags');
$bookmark->save();
return $bookmark;
}
/**
* Display the specified resource.
*
* #param int $id
* #return Response
*/
public function show($id) {
return Bookmark::find($id);
}
/**
* Update the specified resource in storage.
*
* #param int $id
* #return Response
*/
public function update($id) {
$bookmark = Bookmark::find($id);
$bookmark->url = Input::get('url');
$bookmark->description = Input::get('description');
$bookmark->tags = Input::get('tags');
$bookmark->save();
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return Response
*/
public function destroy($id) {
$bookmark = Bookmark::find($id)->delete();
}
}