logout the admin user after delete this admin (laravel 8 ) - php

I want to delete the admin user by just make him password and email as empty Because I need him rest Information in the database
so when i need to delete him I want to change password and email as empty and the same time I want to log him out of the system immediately
every thing works fine but without log him out
here is delete function
public function DeleteAdminRole($id){
$adminimg = Admin::findOrFail($id);
$img = $adminimg->profile_photo_path;
if($img){
unlink($img);
}
Auth::logout($adminimg);
Admin::findOrFail($id)->update([
'type' =>3,
'email'=>"",
'password'=>"",
]);
$notification = array(
'message' => 'Admin User Deleted Successfully',
'alert-type' => 'info'
);
return redirect()->back()->with($notification);
} // end method

Related

Integrating Codeigniter 3 with phpmyadmin database and fullcalendar.io

Hello im using fullcalendar.io for my website, for the framework i use ci3, im new with ci.
So the problem is, can i just load the database fullcalendar.io just for user who logged in?
im planning to use email to connect it since i used email to login to my website..
here is my user database example :
here is my events on fullcalendar.io database :
here is my load function in controllers
function load()
{
$event_data = $this->fullcalendar_model->fetch_all_event();
foreach ($event_data->result_array() as $row) {
$data[] = array(
'id' => $row['id'],
'title' => $row['title'],
'start' => $row['start_event'],
'end' => $row['end_event']
);
}
echo json_encode($data);
}
and here is my fetch_all_event() code
function fetch_all_event()
{
$this->db->order_by('id');
return $this->db->get_where('events');
}
should i adding something or what? can anyone help please im just starting with ci, thank u
So the easiest way is you just have to store ur session data when u log in, store it at the page u login at.
here is mine :
if ($user) {
// jika usernya ada
if ($user['is_active'] == 1) {
if (password_verify($password, $user['password'])) {
$data = [
'email' => $user['email'],
'role_id' => $user['role_id']
];
$this->session->set_userdata($data);
and u can call that email in ur load model, here is my fetch_all_event function code on my model :
function fetch_all_event()
{
$email = $this->session->userdata('email');
$this->db->order_by('id');
return $this->db->get_where('events', ['email' => $email]);
}
this will solve that problem, kept that in mind ur email at database shouldnt do as primary or unique key.

Implementing Payment Gataway in Laravel based shop

I need a little help with implementing payment getaway in Laravel shop.
Payment I use is https://gourl.io/ and I can't understand how to take needed information. So I have set the files database table, database connection and all.. Now I'm trying to redirect user to payment.php page after order form is submitted. This is my CartController.php orderSubmit function
public function orderSubmit() {
$cart = Session::get(self::CART_SESSION_KEY, array());
if (count($cart) < 1) {
return Redirect::to('/');
}
$validatorRules = array(
'captcha' => 'required|captcha',
'shipping_address' => 'required|min:10',
'shipping_method' => 'required|in:' . implode(',', [Settings::SETTINGS_SHIPPING_NORMAL, Settings::SETTINGS_SHIPPING_EXPRESS])
);
Input::merge(array_map('trim', Input::all()));
$validator = Validator::make(Input::all(), $validatorRules);
if ($validator->fails()) {
return Redirect::to('/cart/order?_token=' . csrf_token())->withErrors($validator->errors())->withInput(Input::except(['captcha']));
}
$shipping = array(
'quantity' => 1,
'image' => '/img/noimage.png',
'description' => '',
'title' => 'FIX ME', // this should never occur,
'price' => 100000 // this should never occur
);
switch (Input::get('shipping_method')) {
case Settings::SETTINGS_SHIPPING_NORMAL:
$shipping['title'] = 'Normal Delivery';
$shipping['price'] = 0;
break;
case Settings::SETTINGS_SHIPPING_EXPRESS:
$shipping['title'] = sprintf('Express Delivery - $%.2f', Settings::getOption('express_shipping_cost'));
$shipping['price'] = doubleval(Settings::getOption('express_shipping_cost'));
break;
}
$cart['shipping'] = $shipping;
$order = new Order();
$order->user_id = self::$user->user_id;
$order->data = json_encode($cart);
$order->address = Input::get('shipping_address');
$order->pgp_key = Input::get('gpgkey');
$order->info = Input::get('additional_info');
$order->save();
Session::put(self::CART_SESSION_KEY, array());
return Redirect::to('payment.php')->with('message_success', 'Order created! We will contact you shortly to confirm your order and payment details.');
}
and this is payment.php
require_once( "../cryptobox.class.php" );
/**** CONFIGURATION VARIABLES ****/
$userID = ""; // place your registered userID or md5(userID) here (user1, user7, uo43DC, etc).
// you don't need to use userID for unregistered website visitors
// if userID is empty, system will autogenerate userID and save in cookies
$userFormat = ""; // save userID in cookies (or you can use IPADDRESS, SESSION)
$orderID = "";
$amountUSD = 20;
$period = "NOEXPIRY";
$def_language = "en";
$public_key = "mypublickey";
$private_key = "myprivatekey";
/** PAYMENT BOX **/
$options = array(
"public_key" => $public_key, // your public key from gourl.io
"private_key" => $private_key, // your private key from gourl.io
"webdev_key" => "", // optional, gourl affiliate key
"orderID" => $orderID, // order id or product name
"userID" => $userID, // unique identifier for every user
"userFormat" => $userFormat, // save userID in COOKIE, IPADDRESS or SESSION
"amount" => 0, // product price in coins OR in USD below
"amountUSD" => $amountUSD, // we use product price in USD
"period" => $period, // payment valid period
"language" => $def_language // text on EN - english, FR - french, etc
);
// Initialise Payment Class
$box = new Cryptobox ($options);
// coin name
$coinName = $box->coin_name();
// Successful Cryptocoin Payment received
if ($box->is_paid())
{
if (!$box->is_confirmed()) {
$message = "Thank you for payment (payment #".$box->payment_id()."). Awaiting transaction/payment confirmation";
}
else
{ // payment confirmed (6+ confirmations)
// one time action
if (!$box->is_processed())
{
// One time action after payment has been made/confirmed
$message = "Thank you for order (order #".$orderID.", payment #".$box->payment_id()."). We will send soon";
// Set Payment Status to Processed
$box->set_status_processed();
}
else $message = "Thank you. Your order is in process"; // General message
}
}
else $message = "This invoice has not been paid yet";
$languages_list = display_language_box($def_language);
My question is how to take the correct info in the payment.php? How to take userID, userFormat, orderID and so on?
First of all, I would suggest you use Laravel as the framework it is intended for. In Laravel you define controllers to handle your http-requests. Make a new PaymentController and put the code from payment.php into this controller. Then make a route to that controller-method.
Also put your configuration settings in Laravels config-folder.
And the require_once( "../cryptobox.class.php" ); can be replaced by a dependency injection in your controllers constructor.
Now back to your question.
$userID is where you put your registered Laravel user ID. (If you dont have any registered users, leave it blank). Why you should put your user's id in this variable? -It helps to keep track of which users have done which payments. You can later save this information in your database if you want to keep track of payment history.
$orderID This is where you put your internal order id. Why should you use an internal order id? -Again its to keep track of which purchases of which products have been done by which users. You can store your order-id in your database together with user-id and product-id to get a purchase history log.
$userFormat This is how you wish to store your user information, session, cookie, etc. Because when the purchase is executed, the payment gateway needs a way to access this information, and therefor it must be stored in the session or in a cookie.
I would use $_SESSION['$value'] if you use session for your users!

Updating user with or without password - CakePHP

I try to find a good and clean way to deal with an admin panel "edit user" in cakePHP v.2.7.
To be clear : I want to be able to edit my user with or without overwriting their password, but the cakePHP validator tool don't let me do what I want...
I've already take a look at CakePHP: Edit Users without changing password and Updating user email and password with CakePHP but it seem really dirty :
the first one don't apply the rules onUpdate
the second display the hash (just no... u_u")
There is no other way to do it ? (with as few line as possible)
TL;DR :
View
// add in your view `app/View/Users/edit.ctp`
// a 'fake' field you'll only use on the controller
echo $this->Form->input('new_password');
Controller
// add in your controller `app/Model/User.php#edit()`
// if we have a new password, create key `password` in data
if(!empty($new_password = $this->request->data['User']['new_password']))
$this->request->data['User']['password'] = $new_password;
else // else, we remove the rules on password
$this->User->validator()->remove('password');
Ok, I finally get what I want, here is my code :
On your app/View/Users/edit.ctp you add a field to your form (a custom one, don't add it to your DB)
<?php
// app/View/Users/edit.ctp
echo $this->Form->create('User');
// your other fields
// a 'fake' field you'll only use on the controller
echo $this->Form->input('new_password');
Don't change your app/Model/User.php ; here is mine :
<?php
// app/Model/User.php
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {
public $validate = array(
// [...] other rules
'password' => array(
'passwordLength'=>array(
'rule' => array('minLength', 8),
'message' => 'Too short...',
),
'passwordNotBlank'=>array(
'rule' => 'notBlank',
'required' => true,
'allowEmpty' => false,
'message' => 'A password is required',
),
),
);
public function beforeSave($options = array()) {
if (!empty($pwd = $this->data[$this->alias]['password']))
$this->data[$this->alias]['password'] = AuthComponent::password($pwd);
return true;
}
}
And on your app/Controller/UsersController.php you use this :
<?php
public function edit($id = null) {
$this->User->id = $id;
if (!$this->User->exists())
throw new NotFoundException(__('Invalid user'));
if ($this->request->is('post') || $this->request->is('put')) {
// IMPORTANT >>>>>>>>>>>
// if we have a new password, create key `password` in data
if(!empty($new_password = $this->request->data['User']['new_password']))
$this->request->data['User']['password'] = $new_password;
else // else, we remove the rules on password
$this->User->validator()->remove('password');
// <<<<<<<<<<<<<<<<<<<<<
// then we try to save
if ($this->User->save($this->request->data)) {
$this->Flash->success(__('The user has been updated'));
$this->redirect(array('action' => 'index'));
}
else
$this->Flash->warning(__('The user could not be updated.'));
}
else {
$this->request->data = $this->User->read(null, $id);
unset($this->request->data['User']['password']);
}
}
With the 4 important lines, you are now able to set a new password if needed or disable the validation on the password.
I used this for reference
http://book.cakephp.org/2.0/en/models/data-validation.html#removing-rules-from-the-set

Laravel4: Fetching User data after successful login

After loggin a user, how can I load his data (I want to set his first name in the session)
# Authorize user
if ( Auth::attempt( array('email' => $input['email'], 'password' => $input['password']) ) ) {
// doesnt work: $user = User::where('email',$input['email']);
//
// Load User details here and set in the session
return Redirect::to('/');
} else {
return Redirect::to('/')->with('message', 'Invalid User / Password' );
}
Use this to retrieve the user id -
Auth::user()->id;
and this to get the user instance-
Auth::user();
It will return the instance of the logged in user.
If the first name field is save as first_name column in database, then you can get it like this-
Auth::user()->first_name;
Once a user is authenticated, you may access the User model / record like Auth::user()
For example $email = Auth::user()->email;
Check here for more

De-activate an username and regenerate the same in kohana

In my application,i want to de-activate the existing user and able to create new user with same username and email address.I know,kohana have set username and email address as unique i created a column called is_deactivated in User table and setting a flag to that.If the user is deactivated,the flag set to zero,else the flag is "1".I written an separate function for validation of unique username and email address with reference to is_deactivated flag.If the flag is "0",validation will ignore the existing username and email address and user able to create new user with same existing username and password.New username is created with same username.After signup,it is taking me to existing de-activated user account, instead of fresh new account.But if i login with same username and password,it is taking me to existing de-activated account.In login,i had validated with this is_deactivated flag with 1.
for creating new user with same username i had commented the below lines from User.php,
/var/www/html/zergid/modules/orm/classes/Model/Auth/User.php
public function rules()
{
return array(
// 'username' => array(
// array('not_empty'),
// array('max_length', array(':value', 32)),
// array(array($this, 'unique'), array('username', ':value')),
// ),
'password' => array(
array('not_empty'),
),
// 'email' => array(
// array('not_empty'),
// array('email'),
// array(array($this, 'unique'), array('email', ':value')),
// ),
);
}
Need help to how to login to newly created account and not existing de-activated account.
Thanks
When a user is login to site,it checks the username,password.There i am checking my flag condition.See the code below
/var/www/html/Kohana/modules/orm/classes/Kohana/Auth/ORM.php
protected function _login($user, $password, $remember)
{
if ( ! is_object($user))
{
$username = $user;
// Load the user
$user = ORM::factory('User');
$user->where($user->unique_key($username), '=', $username)->and_where('is_deleted','=',1)->find(); //checking the flag here(->and_where('is_deleted','=',1))
}
Worked!

Categories