I have three tables named users, products and projects.
products and projects have one too many relationships
products have id this id belongs to many projects
This is my products.php table
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->text('detail');
$table->string('color');
$table->string('image');
$table->string('logo');
$table->unsignedBigInteger('user_id');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
This is projects.php thable
Schema::create('projects', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('chapter_name', 255)->nullable();
$table->string('sub_section_name', 500)->nullable();
$table->string('title_1', 255)->nullable();
$table->string('description_1', 5000)->nullable();
$table->string('image_1', 255)->nullable();
$table->string('image_2', 255)->nullable();
$table->string('image_3', 255)->nullable();
$table->string('title_2', 255)->nullable();
$table->string('description_2', 5000)->nullable();
$table->string('title_3', 255)->nullable();
$table->string('description_3', 255)->nullable();
$table->string('video_1', 255)->nullable();
$table->string('video_2', 255)->nullable();
$table->string('video_3', 255)->nullable();
$table->unsignedBigInteger ('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
// $table->foreignId('product_id')->nullable();
$table->unsignedBigInteger('product_id')->references('id')->on('products')->onDelete('cascade');
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->nullable();
});
Here in my ProjectImport.php I want call product_id = id of products table
This is my ProjectImport.php
public function model(array $row)
{
return new Project([
'chapter_name' => $row['chapter_name'],
'sub_section_name' => $row['sub_section_name'],
'title_1' => $row['title_1'],
'description_1' => $row['description_1'],
'image_1' => $row['image_1'],
'image_2' => $row['image_2'],
'image_3' => $row['image_3'],
'title_2' => $row['title_2'],
'description_2' => $row['description_2'],
'title_3' => $row['title_3'],
'description_3' => $row['description_3'],
'video_1' => $row['video_1'],
'video_2' => $row['video_2'],
'video_3' => $row['video_3'],
'user_id' => auth()->user()->id,
'product_id' => Product::where('user_id',Auth::id())->pluck('id') // Here i want product_id = product table id
// 'product_id' => id()->id
]);
}
The relationship is one to many and one to many. Here user have many products with id and every products can have many projects.
This is ProductController.php
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
class ProductController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
// public function indexGetProducts(){
// return User::find(auth()->user()->id)->getProducts;
// }
public function index()
{
// $products = Product::latest()->paginate(20);
$products = Product::where('user_id',auth()->user()->id)->latest()->paginate(20);
return view('products.index',compact('products'))
->with('i', (request()->input('page', 1) - 1) * 5);
// $products= Product::where('user_id',auth()->user()->id)->orderby('created_at','desc')->get();
// return view('products.index',compact('products'))->with('i', (request()->input('page', 5) - 1) * 5);
}
function authapi(Request $request)
{
$user = User:: where('email', $request->email)->first();
if(!$user || !Hash::check($request->password, $user->password)){
return response([
'message' => ['These credentials do not match our records.']
],404);
}
$token = $user -> createToken('my-app-token')->plainTextToken;
$response = [
'user' => $user,
'token' => $token
];
return response($response,201);
}
function all_app_jsons(){
// return Product::all();
return User::find(auth()->user()->id)->getProducts;
}
function search_by_name($name){
return Product::where('name','like','%'.$name.'%')->get();
}
function search_by_id($id){
return Product::where('id',$id)->
where('user_id',auth()->user()->id)->get();
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('products.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//$tag = Product::create($request->all());
//return redirect()->route('admin.tags.index');
$request->validate([
'name' => 'required',
'detail' => 'required',
'color' => 'required',
'image' => 'required|image|mimes:png,jpg,jpeg|dimensions:width=1080,height=1920|max:2048',
'logo' => 'required|image|mimes:png,jpg,jpeg|dimensions:width=512,height=512|max:1024',
]);
$input = $request->all();
// $request->validated();
$input['user_id'] = auth()->user()->id;
if ($image = $request->file('image')) {
$destinationPath = 'image/';
$profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
$image->move($destinationPath, $profileImage);
$input['image'] = "$profileImage";
}
if ($logo = $request->file('logo')) {
$destinationPath = 'logo/';
$profileLogo = date('YmdHis') . "." . $logo->getClientOriginalExtension();
$logo->move($destinationPath, $profileLogo);
$input['logo'] = "$profileLogo";
}
Product::create($input);
return redirect()->route('projects.index')
->with('success','Product created successfully.');
}
/**
* Display the specified resource.
*
* #param \App\Product $product
* #return \Illuminate\Http\Response
*/
public function show(Product $product)
{
return view('products.show',compact('product'));
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Product $product
* #return \Illuminate\Http\Response
*/
public function edit(Product $product)
{
return view('products.edit',compact('product'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Product $product
* #return \Illuminate\Http\Response
*/
// public function update(Request $request, Product $product)
public function update(Request $request, $product_id)
{
$user_id = Auth::user()->id ;
$request->validate([
'name' => 'required',
'detail' => 'required',
'color' => 'required',
]);
$input = $request->all();
if ($image = $request->file('image')) {
$destinationPath = 'image/';
$profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
$image->move($destinationPath, $profileImage);
$input['image'] = "$profileImage";
}else{
unset($input['image']);
}
if ($logo = $request->file('logo')) {
$destinationPath = 'logo/';
$profileLogo = date('YmdHis') . "." . $logo->getClientOriginalExtension();
$logo->move($destinationPath, $profileLogo);
$input['logo'] = "$profileLogo";
}else{
unset($input['logo']);
}
$product_id->update($input);
return redirect()->route('products.index')
->with('success','Product updated successfully');
}
/**
* Remove the specified resource from storage.
*
* #param \App\Product $product
* #return \Illuminate\Http\Response
*/
public function destroy(Product $product)
{
$product->delete();
return redirect()->route('products.index')
->with('success','Product deleted successfully');
}
// function indextwo(){
// //return DB::select("select * from products");
// //DB::table('products')->orderBy('id','desc')->first();
// return Product::orderBy('id', 'DESC')->first();
// }
}
This is ProjectController.php
<?php
namespace App\Http\Controllers;
use App\Exports\UsersExport;
use App\Models\Project;
use App\Imports\ProjectsImport;
use App\Models\Product;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class ProjectController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
// $projects = Project::where('user_id',auth()->user()->id)->latest()->paginate(20);
// $projects = Project::where('user_id',auth()->user()->id)->where('product_id')->latest()->paginate(20);
// $projects = Project::where('user_id',auth()->user()->id)->latest('product_id',8) ->paginate(20);
// $projects = Project::whereIn('product_id',Product::where('user_id',Auth::id())->pluck('id')->toArray())->latest();
// $projects = Project::whereIn('product_id',Product::where('user_id',Auth::id())->pluck('id')->toArray())->latest()->paginate(20);
$projects = Project::whereIn('product_id',Product::where('user_id',Auth::id())->pluck('id')->toArray())->latest()->paginate(20);
return view('projects.index', compact('projects'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('projects.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'chapter_name' => 'required',
'sub_section_name' => 'required',
'title_1' => 'required',
'description_1' => 'required',
'image_1' => 'required',
'image_2' => 'required',
'image_3' => 'required',
'title_2' => 'required',
'description_2' => 'required',
'title_3' => 'required',
'description_3' => 'required',
'video_1' => 'required',
'video_2' => 'required',
'video_3' => 'required',
]);
// $input = $request->all();
// $input['user_id'] = auth()->user()->id;
// $input['product_id'] = $id;
$input = Project::whereIn('product_id',Product::where('user_id',Auth::id())->pluck('id'));
Project::create($input);
return redirect()->route('project.index')
->with('success','Product created successfully.');
}
/**
* Display the specified resource.
*
* #param \App\Models\Project $project
* #return \Illuminate\Http\Response
*/
public function show(Project $project)
{
// $category = $project->category;
return view('projects.show', compact('project'));
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Models\Project $project
* #return \Illuminate\Http\Response
*/
public function edit(Project $project)
{
return view('projects.edit', compact('project'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Models\Project $project
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Project $project)
{
// $user_id = Auth::user()->id ;
$request->validate([
'chapter_name' => 'required',
'sub_section_name' => 'required',
'title_1' => 'required',
'description_1' => 'required',
'image_1' => 'required',
'image_2' => 'required',
'image_3' => 'required',
'title_2' => 'required',
'description_2' => 'required',
'title_3' => 'required',
'description_3' => 'required',
'video_1' => 'required',
'video_2' => 'required',
'video_3' => 'required',
]);
$input = $request->all();
$project->update($input);
return redirect()->route('project.index')
->with('success','Product updated successfully');
}
/**
* Remove the specified resource from storage.
*
* #param \App\Models\Project $project
* #return \Illuminate\Http\Response
*/
public function destroy(Project $project)
{
$project->delete();
return redirect()->route('projects.index')
->with('success', 'Project deleted successfully');
}
public function importProject()
{
Excel::import(new ProjectsImport, request()->file('file'));
return back()->with('success','Project created successfully.');
}
public function export()
{
return Excel::download(new UsersExport, 'projects.xlsx');
}
}
This is user Model user.php
class User extends Authenticatable
{
use HasApiTokens;
use HasFactory;
use HasProfilePhoto;
use Notifiable;
use TwoFactorAuthenticatable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
public function Products(){
return $this->hasMany('App\Models\Product');
}
public function Project(){
return $this->hasMany('App\Models\Project');
}
// public function products(){
// return $this->hasMany(Product::class);
// }
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password',
'remember_token',
'two_factor_recovery_codes',
'two_factor_secret',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* The accessors to append to the model's array form.
*
* #var array
*/
protected $appends = [
'profile_photo_url',
];
}
This is product model product.php
class Product extends Model
{
use HasFactory;
protected $fillable = [
'name', 'detail', 'image','color','logo','user_id'
];
public function User(){
return $this->belongsTo(User::class);
}
public function Project(){
return $this->hasMany('App\Models\Project');
}
}
This is project model project.php
class Project extends Model
{
use HasFactory;
protected $fillable = [
'chapter_name',
'sub_section_name',
'title_1',
'description_1',
'image_1',
'image_2',
'image_3',
'title_2',
'description_2',
'title_3',
'description_3',
'video_1',
'video_2',
'video_3',
'user_id',
'product_id'
];
public function User(){
return $this->belongsTo(User::class);
}
public function Product(){
return $this->belongsTo(Product::class);
}
}
Solved by Project::where('product_id',Product::where('user_id',Auth::id())->pluck('id')->last())->delete();
In ProjectImport
when i create a new record in my yii form with uploaded file, it's working fine, but when i update ihave to attach the file again or else it will give error
here is my controller file, please tell me what is my mistake
my uploaded file is an image, what i want is to change one field let's say the date and keep the rest as is including the uploaded file, but if don't attach the file again it will give an error
<?php
namespace app\controllers;
use Yii;
use app\models\JetskiDamageSettlementAgreement;
use app\models\JetskiDamageSettlementAgreementSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
/**
* JetskiDamageSettlementAgreementController implements the CRUD actions for JetskiDamageSettlementAgreement model.
*/
class JetskiDamageSettlementAgreementController extends Controller
{
/**
* {#inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all JetskiDamageSettlementAgreement models.
* #return mixed
*/
public function actionIndex()
{
$searchModel = new JetskiDamageSettlementAgreementSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single JetskiDamageSettlementAgreement model.
* #param integer $id
* #return mixed
* #throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new JetskiDamageSettlementAgreement model.
* If creation is successful, the browser will be redirected to the 'view' page.
* #return mixed
*/
public function actionCreate()
{
$model = new JetskiDamageSettlementAgreement();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
// get the instance of the uploaded file
$model->damage_image = UploadedFile::getInstance($model, 'damage_image');
$image_name = $model->customer_name.'.'.$model->damage_image->extension;
$image_path = 'attachments/' .$image_name;
$model->damage_image->saveAs($image_path);
$model->damage_image = $image_path;
$model->agreement_date = date ('y-m-d h:m:s');
$model->save();
return $this->redirect(['view', 'id' => $model->agreement_id]);
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Updates an existing JetskiDamageSettlementAgreement model.
* If update is successful, the browser will be redirected to the 'view' page.
* #param integer $id
* #return mixed
* #throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$model->damage_image = UploadedFile::getInstance($model, 'damage_image');
$image_name = $model->customer_name.'.'.$model->damage_image->extension;
$image_path = 'attachments/' .$image_name;
$model->damage_image->saveAs($image_path);
$model->damage_image = $image_path;
$model->save();
return $this->redirect(['view', 'id' => $model->agreement_id]);
}
return $this->render('update', [
'model' => $model,
]);
}
/**
* Deletes an existing JetskiDamageSettlementAgreement model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* #param integer $id
* #return mixed
* #throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the JetskiDamageSettlementAgreement model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* #param integer $id
* #return JetskiDamageSettlementAgreement the loaded model
* #throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = JetskiDamageSettlementAgreement::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}
From what I understand you are getting the error because of your rules set to that model. In your model rules, this field for the file is set to be required for all scenarios.
One possible solution is to set the field to be required only on insert scenario and leave update scenario to be not required for the field. But this really depends on business logic that you need to satisfy.
I am getting this error when I try to run the index method inside the controller:
Declaration of OneTokenAuth::validate() should be compatible with CModel::validate($attributes = NULL, $clearErrors = true)
My controller:
<?php
/**
* Class is used for
*/
class OneTokenAuthController extends Controller
{
public function init()
{
$this->attachbehavior('restBehavior', new RestBehavior());
parent::init();
}
public function filters()
{
return ['accessControl',];
}
public function accessRules()
{
return [
[
'deny',
'actions' => [
'index',
],
'users' => ['#']
]
];
}
/**
* Entry point for validating JWT token
* If the token is valid, user will be logged in as an admin
* and redirected to the admin dashboard
*
* #param [string] $t
* #return void
*/
function actionIndex($t){
$token = CHtml::encode(strip_tags($t));
$auth = new OneTokenAuth($token);
if(!$auth->verify())
die('Token is not valid');
if(!$auth->validate())
die('Token is not valid');
$this->redirect('admin/jobs/dashboardNewest');
}
}
My model:
<?php
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\ValidationData as JWTValidation;
/**
* This is the model class for table "jwt_access_log".
*
* The followings are the available columns in table 'jwt_access_log':
* #property integer $id
* #property text $token
* #property integer $token_status
* #property timespamp $created_at
*/
class OneTokenAuth extends CActiveRecord
{
const VALID = 100;
const UNVERIFIED = 200;
const NONVALID = 300;
private $_singkey;
private $_token;
private $_signer;
private $_data;
function __construct ($token){
$this->_singkey = '1234xxxx';
$this->_signer = new Sha256();
$this->_token =(new Parser())->parse((string) $token);
$this->_token->getHeaders(); // Retrieves the token header
$this->_token->getClaims(); // Retrieves the token claims
$this->_data = new JWTValidation;
$this->_data->setIssuer('http://example.com');
$this->_data->setAudience($this->_token->getClaim('iss'));
}
public function tableName()
{
return 'jwt_access_log';
}
public function rules()
{
return [
['token_status', 'numerical', 'integerOnly' => true],
['token', 'length', 'max' => 1024],
['created_at', 'safe'],
];
}
/**
* #return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'token' => 'Token',
'token_status' => 'Token Status',
'created_at' => 'Created At'
);
}
public function verify(){
if($this->_token->verify($this->_signer, $this->_singkey))
return true;
$this->makeLog(self::NONVALID);
return false;
}
public function validate(){
if($this->_token->validate($this->_data)){
$this->adminLogin();
return true;
}
$this->makeLog(self::UNVERIFIED);
return false;
}
public function makeLog($status)
{
$model = new self();
var_dump('<pre>', $model, '</pre>');die;
$model->setAttributes([
'token' => $this->_token,
'token_status' => $status,
]);
$model->save();
}
private function adminLogin()
{
$this->makeLog(self::VALID);
$login = new LoginComponent([
'email' => 'admin#admin.com',
'password' => 'u4ci_7aM%pigRe]Vp9B',
]);
$login->login();
}
}
What is going here?
The method in the derived class must have the same parameters as the parent class, you must specify parameters $attributes and $clearErrors
public function validate($attributes=null,$clearErrors=true){
if($this->_token->validate($this->_data)){
$this->adminLogin();
return true;
}
$this->makeLog(self::UNVERIFIED);
return false;
}
Below is my code for AuthController
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
//use App\Http\Requests\Request;
use Request;
use View;
use Hash;
use DB;
use Auth;
class AuthController extends Controller
{
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
/**
* Where to redirect users after login / registration.
*
* #var string
*/
protected $redirectTo = '/home';
protected $redirectAfterLogout = '/login';
protected $username = 'user_name';
/**
* Create a new authentication controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}
/**
* Get a validator for an incoming registration request.
*
* #param array $data
* #return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
]);
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
public function showLogin()
{
if (Auth::check())
{
return redirect('/home');
}
else
{
return View::make('index');
}
}
public function doLogin()
{
//echo 'test';
$input = Request::all();
$pass = Hash::make($input['password']);
//print_r($input);exit;
//echo $input['username'];exit;
/*DB::table('admin_user')->insert(
['user_name' => $input['username'], 'password' => $pass]
);*/
if (Auth::attempt(['user_name' => $input['username'], 'password' => $input['password']])) {
return redirect('/home');
//return View::make('home');
}
else
{
return redirect('/');
}
}
public function doLogout()
{
Auth::logout();
return redirect('/');
}
}
Below is my Route Code
Route::get('/',array('uses'=>'Auth\AuthController#showLogin') );
Route::post('/login',array('uses'=>'Auth\AuthController#doLogin'));
//Route::get('/login',array('uses'=>'Login#showLogin') );
Route::group(['middleware' => ['web', 'auth.basic']], function(){
Route::get('/home',['uses'=>'Home#getHome']);
Route::get('/logout',array('uses'=>'Auth\AuthController#doLogout') );
});
i am using user name instead of email id for Auth but below error is shown
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'email' in
'where clause' (SQL: select * from admin_user where email = admin
limit 1)
below is my kernal.php code
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* #var array
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
];
/**
* The application's route middleware groups.
*
* #var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],
'api' => [
'throttle:60,1',
],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* #var array
*/
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];
}
please help me how can i do login with username .
Thanks in advance.
Update:
Add the auth middleware to specific route
Route::group(['middleware' => ['web']], function(){
Route::get('/',array('uses'=>'Auth\AuthController#showLogin') );
Route::post('/login',array('uses'=>'Auth\AuthController#doLogin'));
Route::get('/home',['uses'=>'Home#getHome'])->middleware('auth');//update
Route::get('/logout',array('uses'=>'Auth\AuthController#doLogout') );
});
To redirect to intended page after login replace your doLogin() function with following:
public function doLogin()
{
$input = Request::all();
$pass = Hash::make($input['password']);
if (Auth::attempt(['user_name' => $input['username'], 'password' => $input['password']])) {
return redirect()->intended('/home');//This line is changed
}
else
{
return redirect('/');
}
}
Explaination:
intended() method redirects the user to the previous page, from where the user is redirected to login page. It expects a default route as a parameter, where user will be sent if he has came here directly.
Update 2:
add doLogout in your AuthController's constructor:
public function __construct()
{
$this->middleware($this->guestMiddleware(), ['except' => 'doLogout']);
}
You can simply override $username in AuthController by writing protected $username = 'username'.
I implemented a WSSE client to access my orocrm implementation REST API. It works if I run it on the same server, so I can say that it is correct. It doesn't work if I run it from another server on the same LAN (so I'm sure that only the local httpd server is involved). This is the code and it works locally. Is there some httpd directive to set to manage correctly WSSE header?
<?php
$username = 'admin';
$apiUserKey = '32e4c7a5f3a4c1f59b85be43f2e33dcd5afacbac';
$userSalt = ''; // Will be removed in version 1.0 of OroCRM
$url = 'http://my-server-LAN-IP/crm-application/web/app_dev.php/api/rest/latest/users';
$oroWsse = new OroWsseAuthentification($username, $apiUserKey, $userSalt);
$ch = curl_init();
$headers = $oroWsse->getHeaders();
print_r($headers);
$array = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_HEADER => 0,
CURLOPT_FAILONERROR => true,
CURLOPT_URL => $url
);
curl_setopt_array($ch, $array);
$result = curl_exec($ch);
if ( $result === false) {
echo curl_error($ch);
} else {
echo ($result) . "\n";
}
curl_close($ch);
class OroWsseAuthentification
{
protected $_username;
protected $_apiKey;
protected $_userSalt;
/**
* #param $username
* #param $apiUserKey
* #param string $userSalt
*/
public function __construct ($username, $apiUserKey, $userSalt = '')
{
$this->_username = $username;
$this->_apiKey = $apiUserKey;
$this->_userSalt = $userSalt; // deprecated in OroCRM v1.0
}
/**
* #param $raw
* #param $salt
* #return string
*/
private function _encodePassword($raw, $salt)
{
$salted = $this->_mergePasswordAndSalt($raw, $salt);
$digest = hash('sha1', $salted, true);
return base64_encode($digest);
}
/**
* #param $password
* #param $salt
* #return string
* #throws InvalidArgumentException
*/
private function _mergePasswordAndSalt($password, $salt)
{
if (empty($salt)) {
return $password;
}
if (false !== strrpos($salt, '{') || false !== strrpos($salt, '}')) {
throw new \InvalidArgumentException('Cannot use { or } in salt.');
}
return $password.'{'.$salt.'}';
}
/**
* #return array
*/
public function getHeaders ()
{
// this is my server hostname
$prefix = 'my-server-hostname';
$created = date('c');
$nonce = base64_encode(substr(md5(uniqid($prefix . '_', true)), 0, 16));
$passwordDigest = $this->_encodePassword(base64_decode($nonce) . $created . $this->_apiKey, $this->_userSalt);
$wsseProfile = sprintf(
'X-WSSE: UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"',
$this->_username,
$passwordDigest,
$nonce,
$created
);
return array(
'Authorization: WSSE profile="UsernameToken"',
$wsseProfile
);
}
}
Are you sure that datetime on both servers is the same?
Do you use new WSSE header for each request (as it's required by WSSE spec)?