Component Development - Modifying the OSSNUser class.

Michieal ~ Coder ~ Posted in Component Development 1 week 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
German Arsalan Shah Replied 5 days 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 5 days 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!

German Arsalan Shah Replied 5 days ago

to check that

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

.

us Michieal ~ Coder ~ Replied 6 days ago

Also, I need to know how to hook the user's creation, so that I can add

$user->data->extended_atrrib_1 

at the point of user creation, so that when it's checked later it doesn't return an error.

And, how do I check that? do I use

$user->extended_atrrib_1 

or

$user->data->extended_atrrib_1 

?

Sorry for breaking it up so much, I came back to check to see if I had a response, and found that the system had garbled my message.

us Michieal ~ Coder ~ Replied 6 days ago

Also, I need to know how to hook the user's creation, so that I can add $user->data->extendedatrrib1 at that point, so later, when it's ($user->data->extendedatrrib1) checked, it doesn't return an error.

us Michieal ~ Coder ~ Replied 6 days ago

@Arsalan
Uhm... because I didn't know how to do that? :D

Thank you,
-M.

German Arsalan Shah Replied 7 days ago

Not sure why you wanna update the user class as you can add more stuff to user by following as example

$user = ossn_user_by_guid(1); //replace with id
$user->data->extra_attribute_a = 123;
$user->data->extra_attribute_b = 234;
$user->save();

.