How to implement login and register? - joomla

I have two forms: (login form, register form) and they submitted to my component.
Login form:
I want to check login and pass and set status to "logged" in joomla.
Register form:
I want register new user in joomla.
How to do it?
$inplogin = JRequest::getVar('inplogin');
$inppass = JRequest::getVar('inppass');
???? JFactory::login($inplogin, $inppass); ????
???? JFactory::registeruser($inplogin, $inppass); ????
???

For Login
$result = JFactory::getApplication()->login(array('username'=>$username, 'password'=>$password));
For Registration
require_once JPATH_ROOT.DS.'components'.DS.'com_users'.DS.'models'.DS.'registration.php';
$model = new UsersModelRegistration();
jimport('joomla.mail.helper');
// Attempt to save the data.
jimport('joomla.user.helper');
$username = ; //your user name here
$password = ; //your password here
$data = array( 'username'=>$email,'name'=>$email,'email1'=>$email,'password1'=>$password, 'password2'=>$password, 'block'=>1 );
$return = $model->register($data);

I don't see how this is related specifically to joomla.
you need to use the same $_SESSION variable in both cases. after you declare the variable, you can check on the beginning of your index page if this var exists (and if so, whether the credentials are correct or not). something like:
<?php
session_start();
if (!isset($_SESSION['logged']) {
header("location:login.php");
exit();
}
?>

you can use JUser::bind($userData) and JUser::save($userData) for registering the user.

Expanding upon Gaurav's answer:
If you're using just the Joomla framework and you don't have a site loaded, you will still need to initialize the site.
That said, these two lines should do the trick:
$app = JFactory::getApplication('site');
$app->initialise();

Related

login a user and granting core permissions in Joomla

I am working on a project which requires some manager level access to perform tasks, so when I receive a call I forcefully logging in the request as a superuser so that it will have all permissions to complete that task. For login I am using this code:
function forceLogin($superuserId)
{
$user = JFactory::getUser($superuserId);
//Will authorize you as this user.
JPluginHelper::importPlugin('user');
$options = array();
$options['action'] = 'core.login.site';
$response = new stdClass();
$response->username = $user->username;
$response->language = '';
$response->email = $user->email;
$response->password_clear = '';
$response->fullname = '';
$result = $app->triggerEvent('onUserLogin', array((array)$response, $options));
return true;
}
By this my current login user will be superuser. Now the concern is when any extension is searching for permissions, it is still getting that current session doesn't have them and so it returns false.
One of the solutions I came around is to redirect internally after login and then proceed to other tasks, in that way the system recognizes session to be availed with all permissions. For example -
I received something in getNotification()
function getNotification()
{
//from here I log in the user
$this->forceLogin($speruserId);
//and now redirect
$app = JFactory::getApplication();
$app->redirect('index.php?option=com_mycomponent&task=setNotification');
}
Now I proceed further request from setNotification()
function getNotification()
{
// do my work here
}
To be specific, the issue is arising in VirtueMart (e-commerce extension) in which I am creating a product from my call and while creating a product it checks vmAccess::manager('product.create') which is actually same as core.create of Joomla.
I think by redirecting session is being reset with current user and so it gets all permission. Can it be done without redirection? If yes, how?

What to do when lucadegasperi oauth2 server for laravel gets caught by auth middleware?

So currently building an oauth2 server with:
https://github.com/lucadegasperi/oauth2-server-laravel/blob/master/docs/authorization-server/auth-code.md
Auth Grant
laravel 5.2
Now no where in the instructions does it address what to do when the user is not logged in. (which most times will be the case)
So in that scenario - the user hits the auth middleware kicking them to the login screen... but what to do after that? There is nothing passed to the login page? so how do i know where to redirect the user back to?
Now yes of course I can just do this on my own, but before I do that I just want to make sure I am not missing anything? again it was not address in the documentation, so I can only assume this was thought through?
Let me know your thoughts.
Steve
So just ended up doing a work around in my Authenticate.php file. Incase anyone else is curious I did this:
$params = [];
if($request->has('client_id'))
$params['client_id'] = $request->client_id;
if($request->has('redirect_uri'))
$params['redirect_uri'] = $request->redirect_uri;
if($request->has('response_type'))
$params['response_type'] = $request->response_type;
if($request->has('scope'))
$params['scope'] = $request->scope;
if($request->has('state'))
$params['state'] = $request->state;
return redirect()->route('login', $params);
//return redirect()->guest('login');
Passed this to my loginController. Then in loginController:
$params = [];
if($this->request->has('redirect_uri'))
$params['redirect_uri'] = $this->request->redirect_uri;
if($this->request->has('response_type'))
$params['response_type'] = $this->request->response_type;
if($this->request->has('scope'))
$params['scope'] = $this->request->scope;
if($this->request->has('state'))
$params['state'] = $this->request->state;
if($this->request->has('client_id'))
{
$params['client_id'] = $this->request->client_id;
//dd($params);
return redirect()->route('oauth.authorize.get', $params);
}
Let me know if you see any issues.
Cheers
Citti

Passing get variable in CodeIgniter

How to pass get variable from my view to the controller and to the model also and load to my view again
without the use of form like
$jcid= $row['id']; // this id from other table which is parent.
$s = "SELECT * FROM `jobs` WHERE job_cat=$jcid";
$re = mysql_query($s);
$no = mysql_num_rows($re);
echo $no;
Thanks in advance
Amit
Instead of using get param use like this
This can be your form url
http://example.com/index.php/news/local/metro/crime_is_up
$this->uri->segment(3); // Will give you news
Like wise in place of 3 replace with 4 will give you metro and so on. You can access this in controller.
Uri class in the codeigniter
http://ellislab.com/codeigniter/user-guide/libraries/uri.html
First use
$this->load->helper('url');
And then
$data['$jcid'] = $this->uri->segment(3);

How can we redirect to previous page after login in cibonfire?

How can we redirect to previous page after login in cibonfire?
If some module require login to view, than the code there will check for login and if it is not logged in, it will redirect to login page. After login we want to get back to same module. How can we do so.
Thanks
Before your checking ,A user is already loged in ,Add
$this->session->set_userdata('page_url', current_url());
Add in your login function ,After checking the login data.
if($this->session->userdata('page_url'))
redirect($this->session->userdata('page_url'));
else
redirect('default home page');
At the end of your controller function add this line
redirect('your_view_here', $any_variables_here);
I use sessions for logging in and for navegation tasks like that, because it just makes life a lot simpler. In my controller functions I drop in a line of code like the following:
$this->load_segs(func_get_args(), strtolower(__CLASS__).'/'.__FUNCTION__);
Here's the load segements function:
function load_segs($params, $src){
if ( ! is_array($params) || ! isset($src)){
return FALSE;
}
foreach ($params as $key => $value){
//only include values that don't need encoding
if ($value != urlencode($value)){
break;
}
$src .= '/'.htmlentities($value, ENT_QUOTES, 'UTF-8');
}
$this->session->set_userdata('return', $src);
return $src;
}
You can retrieve the information as follows:
$return = html_entity_decode($this->session->userdata('return'));
redirect($return);

Check if Admin is Logged in Within Observer

I'm attempting to check if the administrator is logged in from an observer. The problem is that while this is easy to do when viewing the admin module, viewing the frontend is another story.
There are several similar questions, but unfortunately none of them provide a working solution for Magento 1.6.2.
I wasn't able to successfully get isLoggedIn() to return true in the admin/session class. I also found out that there is a cookie for both frontend and adminhtml, which may help.
The accepted answer in this related question seems to suggest this may not be possible:
Magento - Checking if an Admin and a Customer are logged in
Another related question, with a solution that didn't help my specific case:
Magento : How to check if admin is logged in within a module controller?
It is possible. What you need to do is switch the session data. You can do this with the following code:
$switchSessionName = 'adminhtml';
if (!empty($_COOKIE[$switchSessionName])) {
$currentSessionId = Mage::getSingleton('core/session')->getSessionId();
$currentSessionName = Mage::getSingleton('core/session')->getSessionName();
if ($currentSessionId && $currentSessionName && isset($_COOKIE[$currentSessionName])) {
$switchSessionId = $_COOKIE[$switchSessionName];
$this->_switchSession($switchSessionName, $switchSessionId);
$whateverData = Mage::getModel('mymodule/session')->getWhateverData();
$this->_switchSession($currentSessionName, $currentSessionId);
}
}
protected function _switchSession($namespace, $id = null) {
session_write_close();
$GLOBALS['_SESSION'] = null;
$session = Mage::getSingleton('core/session');
if ($id) {
$session->setSessionId($id);
}
$session->start($namespace);
}
Late Answer but if as I found it on google:
This it not possible.
Why? Because the default session name in the frontend is frontend and the session name in the backend is admin. Because of this, the session data of the admin is not available in the frontend.
have you tried this :
Mage::getSingleton('admin/session', array('name' => 'adminhtml'))->isLoggedIn();
how about this ( I am not sure it will work or not )
require_once 'app/Mage.php';
umask(0);
$apps = Mage::app('default');
Mage::getSingleton('core/session', array('name'=>'adminhtml'));
$adminSession = Mage::getSingleton('admin/session');
$adminSession->start();
if ($adminSession->isLoggedIn()) {
// check admin
}

Resources