yii if condition within query builder - yii

class Retailjob extends CFormModel {
public function getReatilProducts() {
$condition=false;
$user = Yii::app()->db->createCommand()
->select('tbl_retailjob.retailjobmaster_id, tbl_retailjob.joborderflag, tbl_retailjoborder.retailjob_id, tbl_retailjoborder.retailjobsub_id, tbl_retailjoborder.filename,tbl_retailpostpressjoborder.retailpostpressjo_id,tbl_retailpostpressjoborder.retailjobsub_id,tbl_retailpostpressjoborder.retailpostpresssub_id')
->from('tbl_retailjob')
->join('tbl_retailjoborder', 'tbl_retailjob.retailjobmaster_id=tbl_retailjoborder.retailjobmaster_id')
->join('tbl_retailpostpressjoborder', 'tbl_retailjob.retailjobmaster_id=tbl_retailpostpressjoborder.retailjobmaster_id')
->where('tbl_retailjob.retailjobmaster_id=:id', array(':id' => 7))
->queryAll();
return $user;
}
}
this is my model file
what i want to achieve is if $condition is truethen the where condition should be avoide and if it is false it should be included
can i achieve it like this
public function getReatilProducts() {
$condition=true;
$user = Yii::app()->db->createCommand()
->select('tbl_retailjob.retailjobmaster_id, tbl_retailjob.joborderflag, tbl_retailjoborder.retailjob_id, tbl_retailjoborder.retailjobsub_id, tbl_retailjoborder.filename,tbl_retailpostpressjoborder.retailpostpressjo_id,tbl_retailpostpressjoborder.retailjobsub_id,tbl_retailpostpressjoborder.retailpostpresssub_id')
->from('tbl_retailjob')
->join('tbl_retailjoborder', 'tbl_retailjob.retailjobmaster_id=tbl_retailjoborder.retailjobmaster_id')
->join('tbl_retailpostpressjoborder', 'tbl_retailjob.retailjobmaster_id=tbl_retailpostpressjoborder.retailjobmaster_id')
if ($condition !=true) {
->where('tbl_retailjob.retailjobmaster_id=:id', array(':id' => 7))
}
->queryAll();
return $user;
}
}

Try this:
public function getReatilProducts() {
$condition=true;
$command = Yii::app()->db->createCommand()
->select('tbl_retailjob.retailjobmaster_id, tbl_retailjob.joborderflag, tbl_retailjoborder.retailjob_id, tbl_retailjoborder.retailjobsub_id, tbl_retailjoborder.filename,tbl_retailpostpressjoborder.retailpostpressjo_id,tbl_retailpostpressjoborder.retailjobsub_id,tbl_retailpostpressjoborder.retailpostpresssub_id')
->from('tbl_retailjob')
->join('tbl_retailjoborder', 'tbl_retailjob.retailjobmaster_id=tbl_retailjoborder.retailjobmaster_id')
->join('tbl_retailpostpressjoborder', 'tbl_retailjob.retailjobmaster_id=tbl_retailpostpressjoborder.retailjobmaster_id');
if ($condition !=true) {
$command->where('tbl_retailjob.retailjobmaster_id=:id', array(':id' => 7));
}
$user = $command->queryAll();
return $user;
}

Related

signin page redirecting again to signin page in codeigniter

Controller
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
class Signin extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper('cias');
$this->load->model('home_model');
$this->load->model('signin_model');
}
public function index(){
$this->is_signed_in();
}
function is_signed_in()
{
$is_signed_in = $this->session->userdata('is_signed_in');
if(!isset($is_signed_in) || $is_signed_in != TRUE)
{
// header
$data['logo'] = $this->home_model->get_logo_by_id();
// footer
$data['contact']=$this->home_model->get_contact();
$this->load->view('front/signin');
}
else
{
redirect('front/dashboard');
}
}
public function signinme()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|max_length[128]|trim');
$this->form_validation->set_rules('password', 'Password', 'required|max_length[32]');
if($this->form_validation->run() == FALSE)
{
$this->index();
}
else
{
$email = strtolower($this->security->xss_clean($this->input->post('email')));
$password = $this->input->post('password');
$result = $this->signin_model->sign_in_me($email, $password);
if(!empty($result))
{
$session_array = array('user_id'=>$result->user_id,
'name'=>$result->name,
'email'=>$result->email,
'phone'=>$result->phone,
'is_signed_in' => TRUE );
$this->session->set_userdata('logged_in', $session_array);
redirect('./dashboard');
}
else
{
$this->session->set_flashdata('error', 'Email Address or password mismatch');
$this->index();
}
}
}
}
Model
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
class Signin_model extends CI_Model
{
// This function used to check the login credentials of the user
function sign_in_me($email, $password)
{
$this->db->select('*');
$this->db->from('user_login');
$this->db->where('email', $email);
$this->db->where('isdeleted', 0);
$query = $this->db->get();
$user = $query->row();
if(!empty($user)){
if(verifyHashedPassword($password, $user->password)){
return $user;
} else {
return array();
}
} else {
return array();
}
}
function get_user_info_id($user_id){
$this->db->select('*');
$this->db->from('user_login');
$this->db->where('user_id', $user_id);
$query = $this->db->get();
return $query->row();
}
}
Want to redirect
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
require APPPATH . '/libraries/FrontController.php';
class Dashboard extends FrontController {
public function __construct(){
parent::__construct();
$this->load->helper('cias');
$this->load->model('home_model');
$this->load->model('signin_model');
$this->is_signed_in();
}
public function index(){
$this->load->view("front/dashboard", $data);
}
function signout() {
$this->session->sess_destroy ();
redirect ( 'signin' );
}
}

Property not found in \Illuminate\Contracts\Auth\Authenticatable|nul

Hello I use laravel 8 and backpack 4.1. I get an error: Property not found in \Illuminate\Contracts\Auth\Authenticatable|null, I have user_id in the table 'tenants'. Has somebody an idea, why the variable $ccu is null.
User.php:
public static function getUser()
{
return Auth::guard('backpack')->user();
}
public function tenant(): BelongsTo
{
return $this->belongsTo(Tenant::class);
}
Tenant.php:
vpublic function ccus(): HasMany
{
return $this->hasMany(Ccu::class);
}
public function users():hasMany
{
return $this->hasMany(User::class, 'user_id');
}
DashboardUserController:
public function index()
{
if (backpack_user()->hasRole('admin')) {
$this->data['title'] = trans('backpack::base.dashboard');
$this->data['breadcrumbs'] = [
trans('backpack::crud.admin') => backpack_url('dashboard'),
trans('backpack::base.dashboard') => false,
];
return view('dashboard', $this->data);
} else {
$user=User::getUser();
$ccu = $user->tenant->ccus()->get();
$ccuDiagram = new CcuDiagram($ccu);
$dataForGauge = CcuDiagram::getData($ccu);
$service = Service::find(1);
return view('ccu', ["dataForGauge" => $dataForGauge, "service" => $service]);
}
}
Ccu.php
public function tenant(): HasOne
{
return $this->hasOne(Tenant::Class);
}

How to login using sql password() function in laravel?

I want to login using the sql password() function in laravel. This is because the master database of employee table contains password in the format insert into tbl_name(' ') values (' ', password('abc'));
So I need to use this master table for login so can anyone suggest me as to how can this be possible?
public function login(Request $request) {
// dd($request->all());
if(Auth::attempt([
'tgi' => $request->tgi,
'password' => $request->password
]))
{
// $user = \DB::where('tgi', $request->tgi)->first();
$user = MasterLogin::where('tgi', $request->tgi)->first();
if($user->is_admin() == '1') {
return redirect()->route('dashboard');
}
elseif($user->is_admin() == '0'){
return redirect()->route('home');
}
elseif($user->is_admin() == '3'){
return redirect()->route('manager');
}
}
return redirect()->back();
}
public function validateCredentials(UserContract $user, array $credentials)
{
$plain = $credentials['password'];
return $this->hasher->check($plain, $user->getAuthPassword());
}
In validateCredentials i would like to know how can I pass the password here.
As of now I tried this as said:
public function login(Request $request) {
// dd($request->all());
if(Auth::attempt([
'tgi' => $request->tgi,
'password' => sha1($request->password)
]))
{
$user = User::select("SELECT * FROM emp_username_db WHERE tgi = $request->tgi AND password = sha1('$request->password')");
if (Hash::check(sha1($request->password), $user['password'])) {
// The passwords match...
return redirect()->route('dashboard');
}
}
return redirect()->back();
}
My code that I am working on
class LoginController extends Controller
{
public function login(Request $request) {
//$user = User::where('tgi', $request->tgi)->first();
$result = User::where('tgi',$request->tgi)->where('password',\DB::raw('password("$request->password")'))->exists();
if ($result) {
if($result->is_admin() == '1'){
// Authentication passed...
return redirect()->intended('dashboard');
}elseif($result->admin == '0'){
return redirect()->route('home');
}
elseif($result->admin == '3'){
return redirect()->route('manager');
}
return redirect()->back();
}
}
As SQL default password is hashed using SHA1 so we can compare user's password by using laravel raw query like this.
$result = User::where('tgi',$request->tgi)->where('password',\DB::raw('password("$request->password")'))->exists();
if($result){
your code....
}
It's redirecting to dashboard but getting 302 found.

How to add a custom input field in Admin Product page

How to add custom input field in Product image form using hooks.I am trying to add a new check box in product image form but i do not know how to create it by using modules as well as i am not able to override core product page template.I am createing directory structure inside themes/classic/module/module_name/.....
If some can write the main module php file of Prestashop 1.7 I am very thankful to you.
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class youmodule extends Module
{
protected $config_form = false;
public function __construct()
{
$this->name = 'youmodule';
$this->tab = 'administration';
$this->version = '1.0.0';
$this->need_instance = 0;
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('youmodule');
$this->description = $this->l('youmodule');
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
}
public function install()
{
return parent::install() &&
$this->registerHook('header') &&
$this->registerHook('displayAdminProductsExtra');
}
public function uninstall()
{
return parent::uninstall();
}
public function hookDisplayAdminProductsExtra($params)
{
$id_product = Tools::getValue('id_product');
//YOURCODE
$this->smarty->assign(array(
'yourvariable' => $yourvariabl
));
return $this->display(__FILE__, '/views/templates/admin/product.tpl');
}
}

Laravel 4.2 auth:attempt fails

I can't find this website and I can't figure out what is wrong with my code.
I am trying to get true for var_dump(Auth::attempt()), but it is always false.
This is my controllers method:
public function vartotojoPrisijungimoForma(){
$view = View::make('vartotojai.vartotojoPrisijungimoForma',
array('title'=>'Vartotojo Prisijungimas'));
var_dump(Auth::attempt(array('vardas'=>'aaaaa','pw'=>'aaaaa')));
return $view;
}
In my database the username is stored as vardas and password as pw
My auth.php file looks like this:
<?php
return array(
'driver' => 'eloquent',
'model' => 'Vartotojai',
'table' => 'vartotojai',
'reminder' => array(
'email' => 'emails.auth.reminder',
'table' => 'password_reminders',
'expire' => 60,
),
);
And the model file which is Vartotojai.php, looks like this:
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class Vartotojai extends ModeliuBaze implements UserInterface, RemindableInterface{
protected $table = 'vartotojai';
public $timestamps = false;
protected static $rules = array(
'vardas'=>'required|unique:vartotojai|min:4|regex:/[a-zA-Z]$/',
'pw'=>'required|alpha_num|between:4,8|confirmed',
'pw_confirmation'=>'required|alpha_num|between:4,8'
);
protected static $messages = array(
'required' => ':attribute laukelis tuscias!',
'min'=> ':attribute laukelyje galimas minimalus(:min) simboliu kiekis!',
'between' => ':attribute laukelis gali buti nuo :min - :max simboliu intervale!',
'vardas.regex'=> ':attribute turi atitikti siuos simbolius (a-zA-Z)',
'unique'=> 'Jau vartotojas su tokiu vardu uzregistruotas!',
'alpha_num'=>':attribute laukelyje galima rasyti tik skaicius ir raides!',
'confirmed'=>'Nesutampa slaptazodziai!'
);
protected $hidden = array('password');
public function getAuthIdentifier(){
return $this->getKey();
}
public function getAuthPassword(){
return $this->password;
}
public function getRememberToken(){
return $this->remember_token;
}
public function setRememberToken($value){
$this->remember_token = $value;
}
public function getRememberTokenName(){
return 'remember_token';
}
public function getReminderEmail(){
return $this->email;
}
}
I tried to check Hash:
public function vartotojoPrisijungimoForma(){
$view = View::make('vartotojai.vartotojoPrisijungimoForma',
array('title'=>'Vartotojo Prisijungimas'));
$pw = Vartotojai::find('5');
var_dump(Auth::attempt(array('vardas'=>'aaaaa','pw'=>'aaaaa')));
var_dump(Hash::check('aaaaa',$pw->pw));
return $view;
}
And hash check shows true.
You either need to change your password field from 'pw' to 'password' in your database. Laravel validates the credentials with 'password'.
OR
You could change this function to:
public function getAuthPassword(){
return $this->pw;
}
Then your attempt function should be:
var_dump(Auth::attempt(array('vardas'=>'aaaaa','password'=>'aaaaa')));