How to get global user profile data available in all blade views? - php

I have user with user profile data stored in database and I am able to read that data on user profile page. What I want is to be able to fetch that data (variable is $profile) on any view in my application. What is the best way to do that? Any help is appreciated. Here is my code.
UserProfileController.php
<?php
namespace App\Http\Controllers;
use App\User;
use App\UserProfile;
use Illuminate\Support\Facades\Auth;
class UserProfileController extends Controller
{
/**
* Display the specified resource.
*
* #param \App\Property $property
* #return \Illuminate\Http\Response
*/
public function show($name)
{
$viewer = User::find(Auth::user()->id);
$owner = User::where('name', $name)->first();
// Check if user with given username exists
if($viewer->id !== $owner->id)
{
return abort(404);
}
if(!$profileId = User::getIdFromName($name)){
return abort(404);
}
$profile = UserProfile::profileDetails($profileId, $viewer->id);
//dd($profile);
return view('profile.show', compact('profile'));
}
}
UserProfile.php
<?php
namespace App;
use App\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
class UserProfile extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
public static function profileData($profileId, $viewerId)
{
$user = User::find($viewerId);
$profile = DB::table('users as u')
->select(DB::raw("
data.*,
u.id, u.gender_id, u.name, u.email, u.age, u.premium
"))
->leftJoin('user_profiles as data', 'data.user_id', '=', 'u.id')
->where('u.id', $profileId)
->first();
return $profile;
}
public static function profileDetails($profileId, $viewerId)
{
$profile = static::profileData($profileId, $viewerId);
if ($profile) {
if ($viewerId != $profileId) {
# Viewer is displaying another user's profile.
$myProfile = false;
} else {
$myProfile = true;
}
}
# Populate profile data array
$profile = [
'viewerId' => $viewerId,
'profile_id' => $profileId,
'myProfile' => $myProfile,
'status' => Value::fieldLabel('status', $profile->status),
'first_name' => $profile->first_name,
'last_name' => $profile->last_name,
'job' => $profile->job,
'distance' => $profile->distance,
'city' => $profile->city,
'interested_in' => Value::fieldLabel('interested_in', $profile->interested_in),
'age_from_preference' => $profile->age_from_preference,
'age_to_preference' => $profile->age_to_preference,
'looking_for' => Value::fieldLabel('looking_for', $profile->looking_for),
'personal_quote' => $profile->personal_quote,
'bio_about' => $profile->bio_about,
'postal_code' => $profile->postal_code,
'country' => $profile->country,
'height' => $profile->height,
'orientation' => Value::fieldLabel('orientation', $profile->orientation),
'sexual_preferences' => Value::fieldLabel('sexual_preferences', $profile->sexual_preferences),
'body_type' => Value::fieldLabel('body_type', $profile->body_type),
'relationship_status' => Value::fieldLabel('relationship_status', $profile->relationship_status),
'children' => Value::fieldLabel('children', $profile->children),
'smoke' => Value::fieldLabel('smoke', $profile->smoke),
'education' => Value::fieldLabel('education', $profile->education),
'astro' => Value::fieldLabel('astro', $profile->astro),
'hobbies' => $profile->hobbies,
'workplace' => $profile->workplace,
'sports' => Value::fieldLabel('sports', $profile->sports),
'movies_series' => Value::fieldLabel('movies_series', $profile->movies_series),
'music' => Value::fieldLabel('music', $profile->music),
'food' => Value::fieldLabel('food', $profile->food),
'literature' => Value::fieldLabel('literature', $profile->literature),
'mobile_number' => $profile->mobile_number,
];
return $profile;
}
}

You can use View::share() method to share variable in all views.
Since you want to share authenticated user data it will be good to use middleware for that:
class SomeMiddleware {
public function handle($request, Closure $next) {
$viewer = User::find(Auth::user()->id);
$owner = User::where('name', $name)->first();
// Check if user with given username exists
if($viewer->id !== $owner->id) {
return abort(404);
}
if(!$profileId = User::getIdFromName($name)){
return abort(404);
}
$profile = UserProfile::profileDetails($profileId, $viewer->id);
View::share('profile_details', $profile);
}
/**
* You can get in view like this:
* {{ $profile_details }}
*/
}
** Method 2 **
In your AppServiceProvider You can use view()->composer(). For example:
...
public function boot(){
view()->composer('*', function ($view) {
if(!Auth::check()) {
abort(401);
}
$viewer = User::find(Auth::user()->id);
$owner = User::where('name', $name)->first();
// Check if user with given username exists
if($viewer->id !== $owner->id) {
return abort(404);
}
if(!$profileId = User::getIdFromName($name)){
return abort(404);
}
$profile = UserProfile::profileDetails($profileId, $viewer->id);
View::share('profile_details', $profile);
$view->with('profile_details', $profile);
});
}
...

Related

How do I limit a database field update to once in 24hrs per user in Laravel

I have created an API that records records on the LoginActivity table anytime a user logs in. I want to the recorded activity to be only recorded once in 24hrs for every user.
This is my activity_record.py model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ActivityRecord extends Model
{
protected $fillable = ['user_id','total_logins', 'is_active'];
public function user()
{
return $this->belongsTo(User::class);
}
}
This is my create_activity_record_table.py table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateActivityRecordsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('activity_records', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->integer('total_logins')->default(0);
$table->integer('is_active')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('activity_records');
}
}
This is the UserController.py
public function __construct()
{
$this->middleware('auth:user');
}
public function login(Request $request)
{
$request->validate(['email' => 'required', 'password' => 'required']);
$credentials = ['email' => $request->email, 'password' => $request->password];
if (!$token = auth('user')->attempt($credentials)) {
return response()->json(['success' => false, 'message' => 'Email or Password Is Incorrect'], 401);
}
$user = Auth::guard('user')->user();
if (!$this->UpdateTwentyFours($user)) {
// Work here
if(!$user->is_active){
$activity = ActivityRecord::updateOrCreate(['is_active'=> 0,'user_id'=> auth('user')->user()->id])->increment('total_logins', 0);
}else{
$activity_add = ActivityRecord::updateOrCreate(['is_active'=> 1, 'user_id'=> auth('user')->user()->id])->increment('total_logins', 1);
}
};
return response()->json([
'success' => true,
'auth' => $this->respondWithToken($token),
'user' => new UserResource(User::where(['email' => $request->email])->first()),
]);
}
protected function respondWithToken($token)
{
return [
'access_token' => $token,
'token_type' => 'bearer',
'expires_in' => auth()->factory()->getTTL() * 60
];
}
public function UpdateTwentyFours($user)
{
$user = Auth::guard('user')->user()->id;
$now = Carbon::now()->toDateTimeString();
$activityUpdatedAt = ActivityRecord::where('user_id', $user)->get('updated_at');
$activityAt = $activityUpdatedAt->implode('updated_at');
$diff_in_hours = Carbon::createFromFormat('Y-m-d H:i:s', $activityAt)->format('Y-m-d H:i:s');
$diff_in = new Carbon($diff_in_hours);
$length = $diff_in->diffInHours($now);
if($length > 24){
return false;
}
return true;
}
I keep getting this error anytime I try to use the code above to achieve my purpose
Carbon\Exceptions\InvalidFormatException: Trailing data in file C:\Users\Desktop\PROJECT\vendor\nesbot\carbon\src\Carbon\Traits\Creator.php on line 643
#0 C:\Users\Desktop\PROJECT\vendor\nesbot\carbon\src\Carbon\Traits\Creator.php(665): Carbon\Carbon::rawCreateFromFormat('Y-m-d H:i:s', '2021-04-05 13:1...', NULL)
#1 C:\Users\Desktop\PROJECT\app\Http\Controllers\UserController.php(67): Carbon\Carbon::createFromFormat('Y-m-d H:i:s', '2021-04-05 13:1...')
#2 C:\Users\Desktop\PROJECT\app\Http\Controllers\UserActivityController.php(35): App\Http\Controllers\UserController->UpdateTwentyFours(11)
#3 C:\Users\Desktop\PROJECT\vendor\laravel\framework\src\Illuminate\Routing\Controller.php(54): App\Http\Controllers\UserController->store(Object(Illuminate\Http\Request))
#4 C:\Users\Desktop\PROJECT\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(45): Illuminate\Routing\Controller->callAction('login', Array)
#5 C:\Users\Desktop\PROJECT\vendor\laravel\framework\src\Illuminate\Routing\Route.php(239): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(App\Http\Controllers\UserController), 'login')
Where am I going wrong and is there another way to get the recorded activity to be only recorded once in 24hrs for every user?
I think this is the most clear way to implement update:
class UserController {
public function login(){
...
$this->updateActivityEvery24Hours();
...
}
public function updateActivityEvery24Hours()
{
// Runs given callback function if cache does not exists or 24*60 minutes (24hours) is past
cache()->remember('user_activity_update', 24 * 60, function(){
$user = Auth::guard('user')->user()->id;
ActivityRecord::updateOrCreate([
'is_active'=> !$user->is_active,
'user_id'=> $user->id
])->increment('total_logins', $user->is_active ? 1 : 0);
});
}
}

Multi tenant multi DB can't login to sub domain

I have a multi Tenant / multi DB application & each Tenant has it's own Subdomain with Laravel 7.2
When I go to rootdomain.com/super-admin I can login.
When I go to demo.rootdomain.com/login I can login (default database).
When I go to tenant1.rootdomain.com/login I can NOT login.
I'm using Laravels Auth scaffolding.
I have narrowed it down to the ValidatesRequests trait. If I add the email/password manually I can login to tenant1.rootdomain.com/login
So it looks like this:
public function validate(Request $request, array $rules,
array $messages = [], array $customAttributes = [])
{
return [
"email" => "tenant1#rootdomain.com",
"password" => "#Abc123"
];
return $this->getValidationFactory()->make(
$request->all(), $rules, $messages, $customAttributes
)->validate();
}
Here is my TenantProvider to switch connection for each DB
public function register()
{
if($this->app->runningInConsole()){
return;
}
$host = request()->getHttpHost();
$new_host = explode('.', str_replace('www.', '', $host));
//host must contain at least 3
if(count($new_host) == 3 && $new_host[0] != 'dev'){
config(['database.connections.mysql.database' => 'c4_'.$new_host[0].'_app']);
DB::purge('mysql');
DB::reconnect('mysql');
try {
DB::connection()->getPdo();
if(!DB::connection()->getDatabaseName()){
//reset to default
config(['database.connections.mysql.database' => 'c4_app']);
DB::purge('mysql');
DB::reconnect('mysql');
die("Could not find the database OR Subdomain. Please check your configuration.");
}
} catch (\Exception $e) {
//reset to default
config(['database.connections.mysql.database' => 'c4_app']);
DB::purge('mysql');
DB::reconnect('mysql');
die("Could not open connection to database server. Please check your configuration OR subdomain.");
}
}
// dump('DB Connected...ready to go c4_mvp_app ',DB::connection()->getDatabaseName());
}//end function
And my LoginController
namespace App\Http\Controllers\Auth;
use App\Models\GlobalSetting;
use App\Http\Controllers\Front\FrontBaseController;
use App\Models\Social;
use App\Traits\SocialAuthSettings;
use App\Models\User;
use Carbon\Carbon;
use Froiden\Envato\Traits\AppBoot;
use GuzzleHttp\Client;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Laravel\Socialite\Facades\Socialite;
class LoginController extends FrontBaseController
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers, AppBoot, SocialAuthSettings;
/**
* Where to redirect users after login.
*
* #var string
*/
protected $redirectTo = '/admin/dashboard';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
// dd('Base Login', $this->redirectTo);
parent::__construct();
$this->middleware('guest', ['except' => 'logout']);
}
public function showLoginForm()
{
if (!$this->isLegal()) {
// return redirect('verify-purchase');
}
if ($this->setting->front_design == 1 && $this->setting->login_ui == 1) {
return view('saas.login', $this->data);
}
$this->pageTitle = 'Login Page';
return view('auth.login', $this->data);
}
protected function validateLogin(\Illuminate\Http\Request $request)
{
$setting = GlobalSetting::first();
$rules = [
$this->username() => 'required|string',
'password' => 'required|string'
];
// User type from email/username
$user_email = $request->{$this->username()};
$user = User::where('email', $user_email)->first();
if (!is_null($setting->google_recaptcha_key) && (is_null($user) || ($user && !$user->super_admin))) {
$rules['g-recaptcha-response'] = 'required';
}
if (module_enabled('Subdomain')) {
$rules = $this->rulesValidate($user);
}
$this->validate($request, $rules);
}
public function googleRecaptchaMessage()
{
throw ValidationException::withMessages([
'g-recaptcha-response' => [trans('auth.recaptchaFailed')],
]);
}
public function companyInactiveMessage()
{
throw ValidationException::withMessages([
$this->username() => [trans('auth.companyStatusInactive')],
]);
}
public function validateGoogleRecaptcha($googleRecaptchaResponse)
{
$setting = GlobalSetting::first();
$client = new Client();
$response = $client->post(
'https://www.google.com/recaptcha/api/siteverify',
[
'form_params' =>
[
'secret' => $setting->google_recaptcha_secret,
'response' => $googleRecaptchaResponse,
'remoteip' => $_SERVER['REMOTE_ADDR']
]
]
);
$body = json_decode((string) $response->getBody());
return $body->success;
}
public function login(\Illuminate\Http\Request $request)
{
$setting = GlobalSetting::first();
$this->validateLogin($request);
// User type from email/username
$user = User::where($this->username(), $request->{$this->username()})->first();
// dd('LoginController login 140', $user);
if ($user && !$user->super_admin && $user->company->status == 'inactive' && !$user->hasRole('client')) {
return $this->companyInactiveMessage();
}
// Check google recaptcha if setting is enabled
if (!is_null($setting->google_recaptcha_key) && (is_null($user) || ($user && !$user->super_admin))) {
// Checking is google recaptcha is valid
$gRecaptchaResponseInput = 'g-recaptcha-response';
$gRecaptchaResponse = $request->{$gRecaptchaResponseInput};
$validateRecaptcha = $this->validateGoogleRecaptcha($gRecaptchaResponse);
if (!$validateRecaptcha) {
return $this->googleRecaptchaMessage();
}
}
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
if (
method_exists($this, 'hasTooManyLoginAttempts') &&
$this->hasTooManyLoginAttempts($request)
) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
if ($this->attemptLogin($request)) {
return $this->sendLoginResponse($request);
}
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
$this->incrementLoginAttempts($request);
return $this->sendFailedLoginResponse($request);
}
protected function credentials(\Illuminate\Http\Request $request)
{
//return $request->only($this->username(), 'password');
// dd('credentials 185',$request->{$this->username()});
return [
'email' => $request->{$this->username()},
'password' => $request->password,
'status' => 'active',
'login' => 'enable'
];
}
protected function redirectTo()
{
$user = auth()->user();
if ($user->super_admin == '1') {
return 'super-admin/dashboard';
} elseif ($user->hasRole('admin')) {
$user->company()->update([
'last_login' => Carbon::now()->format('Y-m-d H:i:s')
]);
return 'admin/dashboard';
}
if ($user->hasRole('employee')) {
return 'member/dashboard';
}
if ($user->hasRole('client')) {
return 'client/dashboard';
}
}
/**
* Log the user out of the application.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function logout(Request $request)
{
$user = auth()->user();
$this->guard()->logout();
$request->session()->invalidate();
if (module_enabled('Subdomain')) {
if ($user->super_admin == 1) {
return $this->loggedOut($request) ?: redirect(route('front.super-admin-login'));
}
}
return $this->loggedOut($request) ?: redirect('/login');
}
private function rulesValidate($user)
{
if (Str::contains(url()->previous(), 'super-admin-login')) {
$rules = [
$this->username() => [
'required',
'string',
Rule::exists('users', 'email')->where(function ($query) {
$query->where('super_admin', '1');
})
],
'password' => 'required|string',
];
} else {
$company = getCompanyBySubDomain();
$client = false;
$companies = [];
if ($user && User::isClient($user->id)) {
$client = true;
foreach ($user->client as $item) {
$companies[] = $item->company_id;
}
}
$rules = [
$this->username() => [
'required',
'string',
Rule::exists('users', 'email')->where(function ($query) use ($company, $companies, $client) {
if ($client) {
$query->whereIn('company_id', $companies);
} else {
$query->where('company_id', $company->id);
}
})
],
'password' => 'required|string',
];
}
// dd('rulesValidate 281',$rules);
return $rules;
}
public function redirect($provider)
{
$this->setSocailAuthConfigs();
return Socialite::driver($provider)->redirect();
}
public function callback(Request $request, $provider)
{
$this->setSocailAuthConfigs();
$redirectRoute = module_enabled('Subdomain') ? 'front.workspace' : 'login';
try {
if ($provider != 'twitter') {
$data = Socialite::driver($provider)->stateless()->user();
} else {
$data = Socialite::driver($provider)->user();
}
} catch (\Exception $e) {
if ($request->has('error_description') || $request->has('denied')) {
return redirect()->route($redirectRoute)->withErrors([$this->username() => 'The user cancelled ' . $provider . ' login']);
}
throw ValidationException::withMessages([
$this->username() => [$e->getMessage()],
])->status(Response::HTTP_TOO_MANY_REQUESTS);
}
$user = User::where('email', '=', $data->email)->first();
if ($user) {
// User found
\DB::beginTransaction();
Social::updateOrCreate(['user_id' => $user->id], [
'social_id' => $data->id,
'social_service' => $provider,
]);
if ($user->super_admin == 1) {
\Auth::login($user);
return redirect()->intended($this->redirectPath());
}
\DB::commit();
$user->social_token = Str::random(60);
$user->save();
if (module_enabled('Subdomain')) {
return redirect()->to(str_replace(request()->getHost(), $user->company->sub_domain, route('login')) . '?token=' . $user->social_token);
}
\Auth::login($user);
return redirect()->intended($this->redirectPath());
}
if (module_enabled('Subdomain')) {
return redirect()->route($redirectRoute)->withErrors(['sub_domain' => Lang::get('auth.sociaLoginFail')]);
}
throw ValidationException::withMessages([
$this->username() => [Lang::get('auth.sociaLoginFail')],
])->status(Response::HTTP_TOO_MANY_REQUESTS);
}
}
Let me know if you need any other code...thank you!
The code works fine....turns out it was a caching issue.

Laravel Policy (Too few arguments to function App\Policy)

I am trying to setup my policy for users. However I keep on getting an error of:
Too few arguments to function App\Policies\UserPolicy::update(), 1 passed in /vendor/laravel/framework/src/Illuminate/Auth/Access/Gate.php on line 481 and exactly 2 expected (View: /resources/views/users/index.blade.php)
ErrorException /app/Policies/UserPolicy.php 20
which is on the UserPolicy#update function
When I am logged in as super_admin, it works fine but it throws this error whenever I am logged in as a user of different role.
Below is my current implementation:
UserPolicy
class UserPolicy
{
use HandlesAuthorization;
public function update(User $user, User $userEdit) {
if ($user->id == $userEdit->id) {
return true;
}
return $user->can('update_user');
}
public function before($user, $ability) {
if ($user->hasRole('super_admin')) {
return true;
}
}
}
UsersController
class UsersController extends Controller {
public function __construct() {
$this->middleware('auth');
}
public function edit(User $user) {
$this->authorize('update', $user);
return view('users.edit', [
'user' => User::with('roles', 'level')->find($user->id),
'surveys' => \App\Survey::all(),
]);
}
public function update(UserRequest $request, User $user) {
$this->authorize('update', $user);
$request->save();
session()->flash('success', 'User successfully updated');
// means user is editing his own profile
if (auth()->id() == $user->id) {
return redirect('/dashboard');
} else {
return redirect('/users');
}
}
}
UserRequest
class UserRequest extends FormRequest {
public function authorize() {
return true;
}
public function rules() {
switch ($this->method()) {
case 'POST':
return [
'name' => 'required|string',
'email' => 'required|string|email|max:255|unique:users',
'role' => 'required|exists:roles,id',
'level' => 'required|string',
];
break;
case 'PATCH':
return [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users,email,'.$this->user->id,
'role' => 'sometimes|exists:roles,id',
'level' => 'sometimes|string',
'password' => 'nullable|sometimes|string|min:6|confirmed'
];
break;
default:
break;
}
}
public function save() {
switch (request()->method()) {
case 'POST':
$this->createUser();
break;
case 'PATCH':
$this->updateUser();
break;
default:
break;
}
}
protected function createUser() {
// random generate password
$password = str_random(8);
$user = User::create([
'name' => request('name'),
'email' => request('email'),
'level_id' => request('level'),
'password' => Hash::make($password),
]);
$user->assignRoleById(request('role'));
Mail::to($user)->send(new WelcomeMail($user, $password));
}
protected function updateUser() {
$user = User::findOrFail($this->user->id);
$user->name = request('name');
$user->email = request('email');
if (request('password') != '') {
$user->password = Hash::make(request('password'));
}
if (request('level') != '') {
$user->level_id = request('level');
}
$user->update();
if (request('role') != '') {
$user->roles()->sync([request('role')]);
}
}
}
AuthServiceProvider
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* #var array
*/
protected $policies = [
\App\User::class => \App\Policies\UserPolicy::class,
];
/**
* Register any authentication / authorization services.
*
* #return void
*/
public function boot()
{
$this->registerPolicies();
foreach ($this->getPermissions() as $permission) {
Gate::define($permission->name, function($user) use ($permission) {
return $user->hasRole($permission->roles);
});
}
}
protected function getPermissions() {
return Permission::with('roles')->get();
}
}
In my views file I'm calling
#can('update', App\User::class)
<!-- html code --!>
#endcan
instead of
#can('update', $user)
<!-- html code --!>
#endcan
I was not passing the user instance into the function which was causing the error.
In UserRequest you haven't given any parameters when you call $user->update();. The update() function requires for a UserRequest instance, as well as a User.
Give this a try: $user->update(request()->all(), $user)
Edit:
I would just move the following...
$this->authorize('update', $user);
$request->save();
session()->flash('success', 'User successfully updated');
// means user is editing his own profile
if (auth()->id() == $user->id) {
return redirect('/dashboard');
} else {
return redirect('/users');
}
...to the updateUser() function.

I am getting the error: Indirect modification of overloaded property App\User::$profile has no effect

I have a relationship between two models in my application: Users and Profile. The User model has a hasOne relationship with the profile model. When I try to run an update method from my Profile Controller I get the error: Indirect modification of overloaded property App\User::$profile has no effect.
This is my update method:
public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required|email',
'facebook' => 'required|url',
'youtube' => 'required|url'
]);
$user = Auth::user();
//dd($user);
if($request->hasFile('avatar'))
{
$avatar = $request->avatar;
$avatar_new_name = time() . $avatar->getClientOriginalName();
$avatar->move('uploads/avatars', $avatar_new_name);
$user->profile->avatar = 'uploads/avatars/' . $avatar_new_name ;
$user->profile->save();
}
$user->name = $request->name;
$user->email = $request->email;
$user->profile->facebook = $request->facebook;
$user->profile->youtube = $request->youtube;
$user->profile->about = $request->about;
$user->save();
$user->profile->save();
if($request->has('password'))
{
$user->password = bcrypt($request->password);
$user->save();
}
Session::flash('success', 'Account profile updated.');
return back();
}
This is my User.php file:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password', 'admin'
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function profile()
{
return $this->hasOne('App\Profile');
}
public function posts()
{
return $this->hasMany('App\Post');
}
}
This is my Profile.php file:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Profile extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
protected $fillable = [
'user_id', 'avatar', 'youtube', 'facebook', 'about'
];
}
How do I fix this error?
You need to create a new Profile instance, save it to the database and then call the following method to setup the relationship. At present you are trying to assign properties to something which doesn't exist.
Do the following
$profile = new Profile();
$profile->myvar = 'value';
$profile->save();
Then associate (one of these will work but I haven't tested the code)
$user->profile()->save($profile);
$user->profile()->associate($profile);
You have to make a relational object of $user. You may try with this.
$user = User::where('id', Auth::user()->id)->with('profile')->first();
Instead of $user = Auth::user();
Please replace your update function with this and try again
public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required|email',
'facebook' => 'required|url',
'youtube' => 'required|url'
]);
$user = User::where('id', Auth::user()->id)->first();
if($user) {
$user->name = $request->name;
$user->email = $request->email;
if($request->has('password'))
{
$user->password = bcrypt($request->password);
}
}
if( $user->save() ) {
$profile = Profile::where('user_id', Auth::user()->id)->first();
$profile->facebook = $request->facebook;
$profile->youtube = $request->youtube;
$profile->about = $request->about;
if($request->hasFile('avatar'))
{
$avatar = $request->avatar;
$avatar_new_name = time() . $avatar->getClientOriginalName();
$avatar->move('uploads/avatars', $avatar_new_name);
$profile->avatar = 'uploads/avatars/' . $avatar_new_name ;
}
}
if($profile->save()) {
Session::flash('success', 'Account profile updated.');
} else {
Session::flash('error', 'Failed to update.');
}
return back();
}

Laravel 5.2: Integrate entrust package, create role and permissions and access it

I am totally new in laravel.
I install laravel 5.2 . I have done with CRUD in laravel. Now i want to integrate laravel authentication package. so i choose zizaco\entrust.
I follow each steps from doc link. but i don't understand what is wrong. In doc there is not mentioned that in which file i have to add following code.
$owner = new Role();
$owner->name = 'owner';
$owner->display_name = 'Project Owner'; // optional
$owner->description = 'User is the owner of a given project'; // optional
$owner->save();
$admin = new Role();
$admin->name = 'admin';
$admin->display_name = 'User Administrator'; // optional
$admin->description = 'User is allowed to manage and edit other users'; // optional
$admin->save();
and other below code in document.
Even
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
Not mentioned about implements class.
i do
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
but i got error
Trait 'App\Authenticatable' not found
New learner can't get where to place code. i search alot but i can't get perfect document which give right direction.
Where to create role,permissions? Anyone please help me.
1.Install "zizaco/entrust": "5.2.x-dev"
2.add provider and aliases in app.php
3.php artisan vendor:publish
4.php artisan migrate
Now add in user.php
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Zizaco\Entrust\Traits\EntrustUserTrait;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable, CanResetPassword, EntrustUserTrait;
}
Next Create a model for Role
use Zizaco\Entrust\EntrustRole
class Role extends EntrustRole
{
protected $table = 'roles';
protected $fillable = ['name', 'display_name', 'description']
}
Now create a model for permmission
use Zizaco\Entrust\EntrustPermission;
class Permission extends EntrustPermission
{
protected $table = 'permissions';
protected $fillable = ['name', 'display_name', 'description'];
}
Now create a seeding table for roles and permissions
UserTableSeeder.php
<?php
use Illuminate\Database\Seeder;
use App\Permission;
use App\Role;
use App\User;
class UserTableSeeder extends seeder
{
public function run()
{
Permission::truncate();
Role::truncate();
User::truncate();
\DB::table('role_user')->delete();
\DB::table('permission_role')->delete();
//create a user
$veeru = User::create([
'name' => 'veeru',
'email' => 'something#something.com',
'password' => bcrypt('qwerty'),
]);
//create a role of admin
$admin = Role::create([
'name' => 'admin',
'display_name' => 'Admin',
'description' => 'Only one and only admin',
]);
//create a permission for role
$manage_users = Permission::create([
'name' => 'manage-users-roles-and-permissions',
'display_name' => 'Manage Users,Roles and Permissions',
'description' => 'Can manage users,roles and permission"s',
]);
//here attaching permission for admin role
$admin->attachPermission($manage_users);
//here attaching role for user
$veeru->attachRole($admin);
//here iam creating another role and permisssion
$application = Role::create([
'name' => 'appapirequestlogs',
'display_name' => 'AppApiRequestLogs',
'description' => 'This has full control on Application Core Request logs',
]);
$corereq = Permission::create([
'name' => 'appapireqlogindex',
'display_name' => 'AppApiReqLogIndex',
'description' => 'This has control on Application Core Request Logs index only',
]);
here attaching roles and permissions
$application->attachPermission($corereq);
$veeru->attachRole($application);
}
After seeding that create a routes file and controller for roles and permissions
routes.php
Route::group(['middleware' => 'web'], function () {
Route::group(['prefix' => 'admin'], function () {
Route::controller('roles', 'RolesController');
Route::controller('permissions', 'PermissionsController');
Route::controller('users', 'UsersController');
});
});
RolesController.php
use App\Permission;
use App\Role;
use Illuminate\Http\Request;
use App\Http\Requests;
class RolesController extends Controller
{
function __construct()
{
$this->middleware('auth');
$this->middleware('role:admin');
$this->middleware('permission:manage-users-roles-and-permissions');
}
public function getIndex(Request $request)
{
$roles = Role::with('perms')->get();
return view('roles.index', ['roles' => $roles]);
}
public function getCreate()
{
return view('roles.create');
}
public function postCreate(Request $request)
{
$role = Role::create([
'name' => $request->name,
'display_name' => $request->display_name,
'description' => $request->description
]);
return redirect()->to('admin/roles/index');
}
public function getUpdate($id)
{
$role = Role::findOrFail($id);
return view('roles.update', ['role' => $role]);
}
public function postUpdate(Request $request)
{
$role = Role::findOrFail($request->get('id'));
$role->name = $request->get('name');
$role->display_name = $request->get('display_name');
$role->description = $request->get('description');
$role->save();
return redirect();
}
public function getDelete($id)
{
$role = Role::findOrFail($id);
$role->delete();
return redirect();
}
public function getAttach(Request $request)
{
$role = Role::where('id', '=', $request->id)->with('perms')->first();
$permissions_id = $role->perms->pluck('id')->toArray();
$permissionsNotAttached = Permission::whereNotIn('id', $permissions_id)->get();
return view('roles.attach', compact('role', 'permissionsNotAttached'));
}
public function postAttach(Request $request)
{
$role = Role::findOrFail($request->id);
$permission = Permission::findOrFail($request->permission);
$role->attachPermission($permission);
return redirect();
}
public function getDetach(Request $request)
{
$role = Role::findOrFail($request->role_id);
$permission = Permission::findOrFail($request->permission_id);
$role->detachPermission($permission);
return redirect()->to('/admin/roles/index');
}
}
Similarly create PermissionsController.php
use App\Http\Requests\PermissionRequest;
use App\Permission;
use App\Http\Requests;
class PermissionsController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->middleware('role:admin');
$this->middleware('permission:manage-users-roles-and-permissions');
}
public function getIndex()
{
$permissions = Permission::all();
return view('permissions.index', ['permissions' => $permissions]);
}
public function getCreate()
{
return view('permissions.create');
}
public function postCreate(Request $request)
{
$permission = Permission::create([
'name' => $request->name,
'display_name' => $request->display_name,
'description' => $request->description
]);
return redirect();
}
public function getUpdate($id)
{
$permission = Permission::findOrFail($id);
return view('permissions.update', ['permission' => $permission]);
}
public function postUpdate(Request $request)
{
$permission = Permission::findOrFail($request->get('id'));
$permission->name = $request->get('name');
$permission->display_name = $request->get('display_name');
$permission->description = $request->get('description');
$permission->save();
return redirect();
}
public function getDelete($id)
{
$permission = Permission::findOrFail($id);
$permission->delete();
return redirect();
}
}
After Creating first given process
create roles middleware example CheckRole
<?php
namespace App\Http\Middleware;
use Closure;
class CheckRole
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #param $role
* #return mixed
*/
public function handle($request, Closure $next, $role)
{
if (\Auth::user()->hasRole($role)) {
return $next($request);
} else {
return response()->view('errors.401');
}
}
}
now create Check Permission
<?php namespace App\Http\Middleware;
use Closure;
class CheckPermission
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #param $permission
* #return mixed
*/
public function handle($request, Closure $next, $permission)
{
if (\Auth::user()->can($permission)) {
return $next($request);
} else {
return response()->view('errors.401');
}
}
}
add these middlewares in kernal.php
'role' => CheckRole::class,
'permission' => CheckPermission::class
Now create a role with a name and add permissions to it.
example:
1.create a role name festivals.
2.now create multiple permissions for that
->fesindex (only forr index viewing)
->fesedit (only for edit and update)
->fesadd (only for adding)
->fesdelete (only for deleting)
3.now attach which permissions you want to give to that role festivals
4.if you want a user to see only index page then
for role festivals attach only fesindex
5.if you want a user to see index and add a new festival then create a new role for him and add fesindex and fes add
Now created permissions are placed in your controller
function __construct(FestivalRepository $repo)
{
$this->middleware('auth');
$this->middleware('permission:fesindex', ['only' => 'getFestivals']);
$this->middleware('permission:fesedit', ['only' => ['getFestival', 'postUpdateFestival']]);
$this->middleware('permission:fesadd', ['only' => ['getAddFestival', 'postAddFestival']]);
$this->middleware('permission:fesapprove', ['only' => 'getChangeStatus']);
$this->middleware('permission:fesdelete', ['only' => 'getDeleteFestival']);
}
here getFestivals,getAddFestival etc are methods in controller.

Categories