Jump to content

Get username


lolgc1

Recommended Posts

Where are you trying to get the user information at in the source code? You fetch the user via a call to Users::get, but the context in which you do so will differ based on where you're trying to do this (in a controller, model, plugin, module, gateway, custom code, etc.). So we need to know your Blesta version and more context on what you're doing and where you're doing it at in Blesta to be of more help.

Link to comment
Share on other sites

With custom code outside of Blesta, you should use Blesta's API. You can use the Blesta API SDK to make API requests with your Blesta installation from your custom code.

  1. You will need to create an API user in your Blesta installation to allow API access.
  2. I believe you can fetch the user's ID from the php session's "blesta_id" field in your custom code as long as you are on the same domain.
  3. Pass that "blesta_id" as the user ID to an API request for Users::get to fetch that user from Blesta. The information retrieved from that request will contain the user's username.
Link to comment
Share on other sites

On 7/28/2018 at 1:31 AM, lolgc1 said:

Idk how to get the session "blesta_id".

 

Can you give an example of how to recieve the username, ill continue on working on it because we need more information, i understand things very well when i get an example.

This will help you get the api sorted but then you need Tyson or docs to get the user id: 

 

Link to comment
Share on other sites

normally the signed in user are stored in blesta sessions, so blesta can retrieve the blesta_id . from out side blesta is dificult to get get it or almost impossible to get it with simple code, you need to make plugin to help you do the job or you need to hack some blesta files . 

my simple approach for this would to create a simple plugin that act the login and the logout,  preaction events, and each event i will pass the client info to a remote file (let say data.php). the data php should be in your root folder and use the same sessions as your index.php , the rule of this file would be only to insert in the session the info send from the plugin and destroy the info if sent a command from blesta plugin .

Link to comment
Share on other sites

If you are unable to access the blesta_id from the session:

$user_id = (isset($_SESSION['blesta_id']) ? $_SESSION['blesta_id'] : null);

...then I would suggest following @Blesta Addons's recommendation of creating a plugin.

If you upgrade Blesta to v4.3.0+ then your plugin can make use of the new Requestor object that retrieves some relevant information on the currently-logged-in user. If the user is currently logged into Blesta, you should be able to retrieve their user information by creating a plugin with a model that fetches the user from the database, e.g.:

class MyPluginUserModel extends MyPluginModel
{
	public function getCurrentUser()
	{
		// Load information on the current user
		$requestor = $this->getFromContainer('requestor');

		// Fetch additional information on the current user
		if ($requestor->user_id !== null) {
			Loader::loadModels($this, ['Users']);

			// Fetch and return the user if one exists
			if (($user = $this->Users->get($requestor->user_id))) {
				return $user;
			}
		}

		// Return null that no user was found
		return null;
	}
}

You just need to make an API call to your "MyPluginUserModel::getCurrentUser" to retrieve the user. If a non-null value is returned, the username would be available in the "username" property:

<?php
// Load the API
require_once "/home/username/public_html/blesta_api.php";

$user = "API USERNAME";
$key = "API KEY";
$url = "https://blestainstallationurl.com/api/";
$api = new BlestaApi($url, $user, $key);

// Fetch the current user's username if they are logged into Blesta
$username = null;
if (($user = $api->get("MyPlugin.MyPluginUserModel", "getCurrentUser"))) {
  $username = $user->username;
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...