Login issues - We're new to Concrete5

Permalink
Hi All,

A member of my team has recently created a PHP Tool that works really well in a number of sites. We have been asked to introduce this into a website that uses concrete5. What we would like to do is use the User ID in a members area.

As we have no idea how Concrete5 works, we're getting Access Denied when using the GetUserID function. We have tried all sorts of links to get access to the library to no avail. I'm sure we are mssing something quite fundamental.

Any pointers would be appreciated.

All we are after is.
1. Get the current user.
2. Confirm the user is logged on.

The additional Tool is a stand alone set of php pages under a sub directory in the website.

These are the two separate areas of code the team is using.

$u = new User();
$ui = UserInfo::getByID($u->getUserID());

$isLoggedIn = User::isLoggedIn();
if ($isLoggedIn)
{ echo '<p>Thanks for logging in!</p>';}
else
{ echo '<a href="/login">Login</a>';}

Thanks,

Ali

 
hutman replied on at Permalink Reply
hutman
Not sure I completely understand what you're trying to do, but you should be able to check for a logged in user by doing

$u = new User();
if($u->isLoggedIn()){
//user is logged in
$ui = Core::make(\Concrete\Core\User\UserInfoRepository::class)->getByID($u->getUserID());
} else {
//user is not logged in
}
AliKnox replied on at Permalink Reply
Thanks Hutman,

This code looks like it will do what we want, but nothing happens when entered as though the PHP code is erroring.

We have no includes to library etc..

The website already uses concrete and logs in ok. Our tool was developed outside of the solution and is self contained. The pages have been loaded into a sub directory in the root directory. All we are trying to do is when the user visits the page, check to see if the user is logged in and if so get the UserID and then our tool will do the rest.

But any call we use doesn't seem to work, thats why we are thinking theres some kind of library we should be using.

Really appreciate your help.
AliKnox replied on at Permalink Reply
The structure

/concrete/core/user

doesn't exist.
hutman replied on at Permalink Reply
hutman
Ah... I understand, I thought you were trying to build something IN concrete5, you're not going to be able to use Concrete classes from an outside thing (I don't think). You'll have to create a route within Concrete5 that returns you the UserID instead of trying to get it directly. I can create you a quick package to do that if you want.
AliKnox replied on at Permalink Reply
If you could that would be great, we've been trying to get it to work for days.
hutman replied on at Permalink Reply 1 Attachment
hutman
Try this package. What this does is it creates a route at "/user-integrator/get-current-user" which either echos the currently logged in userID or false if the user is not logged in. You should be able to do a file_get_contents or a cURL call or something from your tool to that URL on the site and get the userID that you wanted.

Make sure you unzip this and place the package in the /packages directory so the structure looks like /packages/user_integrator/controller.php
AliKnox replied on at Permalink Reply
Thank you so much, this sounds/looks like what we're looking for.

We'll test it and come right back.

Really appreciate your help.

Ali
AliKnox replied on at Permalink Reply
Hi Hutman,

Sorry to be a pain, but we're getting a 500 error when trying to deploy the package. I've been told the branch were using is 5.6, which we know will become legacy.

Any help would be appreciated.
hutman replied on at Permalink Best Answer Reply
hutman
Ok, yeah, that package will not work with that version. I assumed you were using a new version since this thread is in the v7+ forum.

That does make things somewhat easier though. Just put a file named get_user_id.php in the root tools directory with this in it

<?php         
defined('C5_EXECUTE') or die(_("Access Denied."));
$user = new User();
if($user->isLoggedIn()){
    echo $user->getUserID();
} else {
    echo false;
}


Then use the URL "/tools/get_user_id"
AliKnox replied on at Permalink Reply
Thanks Hutman, we're getting Access Denied on the defined(C5_execute line.
AliKnox replied on at Permalink Reply
Hutman, you're an absolute star it works. Ignore my last comment, trouble with my fingers.