Advertisement:

Author Topic: osc_user_username - Helper not exists when user logged like admin have  (Read 226 times)

fog

  • Hero Member
  • *****
  • Posts: 1062
I really need get the username value of logged user on all different profile pages, and not only username belonging of current user page.

on hUsers.php file I see this helpers:

Code: [Select]
<?php /**
     * Gets logged admin username
     *
     * @return string
     */
    
function osc_logged_admin_username() {
        return (string) 
Session::newInstance()->_get('adminUserName');
    }

    
/**
     * Gets logged admin name
     * @return string
     */
    
function osc_logged_admin_name() {
        return (string) 
Session::newInstance()->_get('adminName');
    }

But on user side only exists the helper:

    /**
     * Gets logged user name
     *
     * @return string
     */
    
function osc_logged_user_name() {
        return (string) 
Session::newInstance()->_get('userName');
    }
   
   But not exists the function to username to logged user, like this:
Code: [Select]
<?php osc_logged_user_username();   
   Exists some trick to get username of logged user on a different user profile page when user are logged in?
   
   Any path to solution is very welcome
   Thank you

WEBmods

  • Hero Member
  • *****
  • Posts: 937
  • github.com/webmods-croatia/love-osclass/ | patrick
Hello,

I don't think there is any other way but using the User model and getting data by osc_logged_user_id(). Maybe you can save username in session with the after_login hook so you don't need to access the database every time.

Code: [Select]
<?php
// Find username from database.
$user User::newInstance()->findByPrimaryKey(osc_logged_user_id());
$username $user['s_username'];

// Save username in session when logged in.
function wm_login_save_username($user$redirect) {
    
$username $user['s_username'];
    
Session::newInstance()->_set('loggedUsername'$username);
}
osc_add_hook('after_login''wm_login_save_username');

// Get username from session.
function wm_logged_username() {
    return 
Session::newInstance()->_get('loggedUsername');
}

Regards.

fog

  • Hero Member
  • *****
  • Posts: 1062
Hi @patrick, thanks

I did some similar

Code: [Select]
<?php
function user_store_username(){
if(osc_is_web_user_logged_in() == true){
if(Session::newInstance()->_get('logged-username') == ''){
header('Location: ' osc_base_url(true).'?page=user&action=pub_profile&id='osc_logged_user_id());
Session::newInstance()->_set('logged-username'osc_user_username());
}
}
}
osc_add_hook('header''user_store_username');
function 
user_get_logged_username(){
return Session::newInstance()->_get('logged-username');
}

I not tried you code yet, because I saw it now. Anyway, I see a variable not defined "$redirect". I think you put here something already done for you (copy/paste)... sorry if not was the case. Anyway I will see better your code soon.

Thank you

fog

  • Hero Member
  • *****
  • Posts: 1062
I had forgot to find primary key of user ID on certain pages:
Code: [Select]
User::newInstance()->findByPrimaryKey(osc_logged_user_id());
So that works fine after login in any page.

Code: [Select]
<?php
$user 
User::newInstance()->findByPrimaryKey(osc_logged_user_id());
$username $user['s_username'];
Session::newInstance()->_set('loggedUsername'$username);
echo 
Session::newInstance()->_get('loggedUsername');
?>

Thanks again  ;)

WEBmods

  • Hero Member
  • *****
  • Posts: 937
  • github.com/webmods-croatia/love-osclass/ | patrick
Glad you got it working. I checked after_login hook and it sends two variables, user and url_redirect...

Regards.

fog

  • Hero Member
  • *****
  • Posts: 1062
I see. Thanks patrick  :)