Component Development - Modifying the OSSNUser class.

Michieal ~ Coder ~ Posted in Component Development 4 years ago

I was reading through the documentation, and it said not to modify the base classes, as it will be over written in the future. (Of which, I understand that.)
However, I want to add data to the actual user. I want to add things like access level, which groups that they moderate, etc.

I tried copying the OSSNUser.php class file to a classes directory in my component, and then registering the class in the com file (like how OSSNGroups does it) but it didn't work.
Is there a way to do this, or hook it, or something? I need to be able to update the data points at key times like user creation, deletion, etc.

Thank you!

Replies
us Michieal ~ Coder ~ Replied 2 years ago

My one remaining question is - how do I remove the extended attributes, so that I can put that into the disable / delete call back for the component?
Do I just unset it, and save? or is there something specific that I need to do?

Again, thank you for the help!

us Michieal ~ Coder ~ Replied 2 years ago

Okay, I did a before and after dump.
Before, it goes into the [datavars] array; the [data] array is empty.
After, it's a User level variable, like $user->first_name is. the [data] and [datavars] arrays are both empty...
Again, thank you! that single line is super helpful!!!!

Okay, so - I just evaluate them using the $user object ->variable and it works. Cool!

us Michieal ~ Coder ~ Replied 2 years ago

AWESOME! Thank you!!!

German Michael Zülsdorff Replied 2 years ago

Use

error_log('MY_OBJECT_OF_INTEREST ' . ossn_dump($object));

Same goes for arrays.

us Michieal ~ Coder ~ Replied 2 years ago

Z Man (do you prefer that, or Michael?)

because I am not sure how to dump the entire object... My attempts have yielded "object" without details, and I am not sure of the complete structure to do it by fields. Php is not my first language.

do you have a code snippet? I'd love to know.

Thank you!

German Michael Zülsdorff Replied 2 years ago

Why don't you simply dump the entire $user object to your error_log in order to verify what's set and not set?

us Michieal ~ Coder ~ Replied 2 years ago

@Arsalan - I'm having difficulty getting the system to save the extended user data attribute.

    $user = ossn_user_by_guid($params['guid']);
    if($user){
        if (isset($user->data->extended_attrib_1)){
            error_log ("User data status is set");
        } else {
            error_log ("User data status is not set");
        }
        error_log ("Attribute on User: {$user->fullname}");
        $user->data->extended_attrib_1 = 1;
        $user->data->extended_attrib_2 = 2;
        $user->fullname = $user->fullname . " ❌";
        error_log ("Attribute on User (set): {$user->fullname}");
        if (isset($user->data->extended_attrib_1)){
            error_log ("User data status is set");
        } else {
            error_log ("User data status is not set");
        }
        $user->save();
    }

When I do this, and then test it with error_log("{$user->data->extended_attrib_1}"); I get nothing in the log, except a blank entry with the timestamp.
If I test for isset() on the attribute, and log it as "attribute is set to:" and "attribute is not set" it always says that the attribute is not set.

Example log file:

[04-Feb-2022 13:39:21 America/New_York] User data status not set
[04-Feb-2022 13:39:21 America/New_York] Attribute on User: Testy Testeriffic
[04-Feb-2022 13:39:21 America/New_York] Attribute on User (set): Testy Testeriffic ❌
[04-Feb-2022 13:39:21 America/New_York] User data status not set

the ❌ on the user name means that it made it to the end, right before the last log and the function returns. Also checking afterwards (because the above is in the create new user), the attribute $user->data->extended_attrib_1 is not set.

Am I doing something wrong here?

Indonesian Arsalan Shah Replied 4 years ago

This can be done using custom component

function my_custom_component(){
        ossn_register_callback('user', 'created', 'add_my_custom_attrs'); 
}
function add_my_custom_attrs($callback, $type, $params){
        //do not use the return in this function please
        $user = ossn_user_by_guid($params['guid']);
        if($user){
            $user->data->extended_attrib_1 = 123;
            $user->data->extended_attrib_2 = 234;
            $user->save();
        }
}
ossn_register_callback('ossn', 'init', 'my_custom_component');

.

us Michieal ~ Coder ~ Replied 4 years ago

@Arsalan,

Okay, so the attribute gets added as a dynamic property. cool.

Still not clear on how to make sure that the code is ran at user creation. When the system calls ->AddUser, I need to make sure that my extended attribute gets added.

Thank you!

Indonesian Arsalan Shah Replied 4 years ago

to check that

$user = ossn_user_by_guid(1);
echo $user->extended_attrib_1;

.