solved Dynamically changing $_user quota settings

TiG
TiG
@tig
6 years ago
184 posts
What is the recommended method for dynamically changing a
$_user
quota setting such as
$_user['quota_jrSmiley_allowed']
?

We have a need to adjust select quota '_allowed' settings on a per user basis in real time. For example, some users will (conditionally) not have access to smileys. This is not something that can be handled by manually reassigning their quotas; we need to have a programmatic way to make these changes. Basically to conditionally override select fields in their quota on a per user basis.

Is this possible? If so, which listener should I use?

Thanks


--
TiG

updated by @tig: 03/01/19 09:46:43PM
paul
@paul
6 years ago
4,325 posts
The problem with this is that even if you did dynamically change users' quotas by listeners or whatever, their active quota is part of their session, so they would need to log out then back in to get the change.
You might need to apply these conditions at a template level to achieve what you want?
Maybe you can explain more what you want to do (and why) so that we can advise better.
Thanks


--
Paul Asher - JR Developer and System Import Specialist
TiG
TiG
@tig
6 years ago
184 posts
Hi Paul

This would never change the stored quota itself (because that quota applies to many users - not just the users of interest). I only want to change that which has been processed as part of the $_user array and only for select users. ( The quota might apply to 100 users but this change might affect only 2 of them. )

Here is what we are trying to accomplish - using smileys as the example:

Some users will lose smiley privileges (due to abuse). We want a moderator to be able to simply note this in the moderator console (an extant custom module of NT) and then have it go into effect. The moderator has no idea of the underlying quota mechanics - just wants to disable the privilege for a period of time (the moderator console has expiration builtin).

Since tinymce (in this example) renders the smiley button if it finds
$_user['quota_jrSmiley_active']=='on'
if we could simply set this to 'off' the desired effect will be cleanly implemented for that user.

Given there is no intent to change the stored quota itself - just adjust the in-memory quota as represented in $_user for select users - I need an appropriate listener with which to turn the $_user quota item to 'off' for a given user at the proper time.

Thanks


--
TiG

updated by @tig: 11/29/18 02:02:12PM
michael
@michael
6 years ago
7,692 posts
Probably the function that you're after is:
jrUser_session_sync($_user['_user_id']);
That will make sure the $_SESSION is inline with what is in the database for that user_id.
TiG
TiG
@tig
6 years ago
184 posts
Hi Michael

What I am looking for is an event right after $_user is created (or updated) that would allow me to override fields such as $_user['quota_jrSmiley_active'] for a tiny minority of users.

Thanks


--
TiG
michael
@michael
6 years ago
7,692 posts
The event that returns the $_user variable back into router.php is 'session_started'.

You get the $_SESSION data passed into that listener and whatever you return becomes your $_user.

--edit--
in your modules _init()
jrCore_register_event_listener('jrUser', 'session_started', 'jrYourModule_session_started_listener');

updated by @michael: 11/29/18 11:13:42PM
TiG
TiG
@tig
6 years ago
184 posts
Michael

Perfect! Thanks much for the help.


--
TiG
TiG
TiG
@tig
6 years ago
184 posts
Michael

The session_started listener is an elegant method for accomplishing my intent. The change works fine but only for a short time. Trouble is, any change to $_user quota_ fields is overwritten later on by calls to jrProfile_get_quota() whose results are re-assigned to $_user.

Best I can tell the only way to dynamically override a quota field in $_user and make it stick is to do so within this function. I see no customer appropriate means to override a quota_ field in $_user that will survive long enough to have an effect.

Largely this is an FYI, but if anything else comes to mind please let me know.

Thanks,


--
TiG
michael
@michael
6 years ago
7,692 posts
jrProfile_get_quota() doesnt look like it touches $_user.

'session_started' is not such a commonly used listener judging by a code search. One set that is commonly used is:
* 'db_get_item'
* 'db_search_items'

eg:
    // Expand Poll options
    jrCore_register_event_listener('jrCore', 'db_get_item', 'jrPoll_db_get_item_listener');
    jrCore_register_event_listener('jrCore', 'db_search_items', 'jrPoll_db_search_items_listener');

Might try tweaking the data via both of those.

--edit--
Why: because the session start gets its info from:
jrCore_db_get_item('jrUser', $uid, true, true)) {.........
so you'd be tweaking before it arrived at the session start.
updated by @michael: 11/30/18 11:59:15PM
TiG
TiG
@tig
6 years ago
184 posts
Michael

Tweaking using db_get_item appears to cover the bases. Not sure why (difficult to actually trace this to know precisely why this works) but if it works it works, right?

For completeness, here are some details per your last note:

Quote: jrProfile_get_quota() doesnt look like it touches $_user.

jrProfile_get_quota() does indeed not touch $_user but it is called by functions which assign its return array to $_user. So basically one could effect the desired change within jrProfile_get_quota. The following code does indeed accomplish the effect (see @@@):

    $tb1 = jrCore_db_table_name('jrProfile', 'quota_setting');
    $tb2 = jrCore_db_table_name('jrProfile', 'quota_value');
    $req = "SELECT s.`module` AS m, s.`name` AS k, s.`default` AS d, v.`value` AS v FROM {$tb1} s LEFT JOIN {$tb2} v ON (v.`quota_id` = '{$quota_id}' AND v.`module` = s.`module` AND v.`name` = s.`name`)";
    $_rt = jrCore_db_query($req, 'NUMERIC');
    if (!isset($_rt) || !is_array($_rt)) {
        return false;
    }
    $_qt = array();
    foreach ($_rt as $_v) {
        $_qt["quota_{$_v['m']}_{$_v['k']}"] = (isset($_v['v']) && strlen($_v['v']) > 0) ? $_v['v'] : $_v['d'];
    }

    // @@@
    $_qt['quota_jrSmiley_allowed'] = 'off';
    // @@@

My challenge has been to find a way to effect this change with customer appropriate code.

Quote: ... so you'd be tweaking before it arrived at the session start.

The session_started listener did work great -changes applied and accepted into $_user- but was too early in the process. Myriad functions call jrProfile_get_quota() and overwrite the $_user quota fields. So tweaking things before the session start will not work.

But it appears I have a solution now so these other attempts are just history.

As always, I appreciate your insight and support. Thanks,


--
TiG

Tags