FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » FUDforum » How To » Adding users to a group using a script
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Adding users to a group using a script [message #30191] Fri, 10 February 2006 11:44 Go to next message
djechelon is currently offline  djechelon   Italy
Messages: 46
Registered: July 2005
Karma: 0
Member
Hi,
I need to let users add themselves to a group using an external script made by me. The user inserts username/password and a secret code.
This group lets the user access to some forums that are normally visibile but not readable

If everything is OK I run the following SQL Query:

$gid = 64;
$group_opts = 312975;
$sql = "INSERT INTO Forum_group_members (user_id , group_id , group_members_opt) VALUES ('".$uid."', '".$gid."', '".$group_opts."');";

$uid is taken from database and is obviously the user's ID.

When I go to Groups Administration I see the user listed with all permissions correctly set, but he can't still access the restricted forums!!!

Do I have to run other queries?
Thank you in advance.
Re: Adding users to a group using a script [message #30201 is a reply to message #30191] Sat, 11 February 2006 20:03 Go to previous messageGo to next message
Ilia is currently offline  Ilia   Canada
Messages: 13241
Registered: January 2002
Karma: 0
Senior Member
Administrator
Core Developer
You need to rebuild the group cache for that user by using the grp_rebuild_cache() function inside groups.inc passing the user id as the parameter to the function.

FUDforum Core Developer
Re: Adding users to a group using a script [message #30308 is a reply to message #30191] Thu, 16 February 2006 16:45 Go to previous messageGo to next message
djechelon is currently offline  djechelon   Italy
Messages: 46
Registered: July 2005
Karma: 0
Member
Sorry for answering so late but I've been a bit busy.

I have copied and rewritten the function you mentioned, making it rebuild the whole forum's group cache.
This is the result:

<?php
function grp_rebuild_cache($user_id=null) {
        global 
$conn;

        
$list = array();
        
$lmt '';

        
/* generate an array of permissions, in the end we end up with 1ist of permissions */
        
$r mysql_query("SELECT gm.user_id AS uid, gm.group_members_opt AS gco, gr.resource_id AS rid FROM Forum_group_members gm INNER JOIN Forum_group_resources gr ON gr.group_id=gm.group_id WHERE gm.group_members_opt>=65536 AND (gm.group_members_opt & 65536) > 0" . ($lmt ' AND '.$lmt ''),$conn) or die(mysql_error().__LINE__);
        while (
$o mysql_fetch_object($r)) {
                foreach (
$o as $k => $v) {
                        
$o->$k = (int) $v;
                }
                if (isset(
$list[$o->rid][$o->uid])) {
                        if (
$o->gco 131072) {
                                
$list[$o->rid][$o->uid] |= $o->gco;
                        } else {
                                
$list[$o->rid][$o->uid] &= $o->gco;
                        }
                } else {
                        
$list[$o->rid][$o->uid] = $o->gco;
                }
        }
        unset(
$r);

        
$tmp = array();
        foreach (
$list as $k => $v) {
                foreach (
$v as $u => $p) {
                        
$tmp[] = $k.",".$p.",".$u;
                }
        }

        if (!
$tmp) {
                
mysql_query("DELETE FROM Forum_group_cache" . ($lmt ' WHERE '.$lmt ''),$conn) or die(mysql_error().__LINE__);
                return;
        }

        
$sql "REPLACE INTO Forum_group_cache (resource_id, group_cache_opt, user_id) VALUES (".implode('), ('$tmp).")";
        
mysql_query($sql,$conn) or die(mysql_error().$sql);
        
mysql_query("DELETE FROM Forum_group_cache WHERE ".($lmt $lmt ' AND ' '')." id < LAST_INSERT_ID()",$conn) or die(mysql_error().__LINE__);

        
mysql_query("DELETE FROM Forum_group_cache" . ($lmt ' WHERE '.$lmt ''), $conn) or die(mysql_error().__LINE__);
        foreach (
$tmp as $ttmp) {
                
mysql_query("INSERT INTO Forum_group_cache (resource_id, group_cache_opt, user_id) VALUES (".$ttmp.")",$conn) or die(mysql_error().__LINE__);
        }

}
?>


I made it run without errors, but still users can't access the private Forums I mentioned before, even if they are listed in Groups Administration.

Running Consistency check is useless...
What should I do then?
Re: Adding users to a group using a script [message #30316 is a reply to message #30308] Fri, 17 February 2006 14:44 Go to previous messageGo to next message
Ilia is currently offline  Ilia   Canada
Messages: 13241
Registered: January 2002
Karma: 0
Senior Member
Administrator
Core Developer
Are there entries for the relavent users inside the group_cache table?

FUDforum Core Developer
Re: Adding users to a group using a script [message #30320 is a reply to message #30191] Fri, 17 February 2006 15:22 Go to previous messageGo to next message
djechelon is currently offline  djechelon   Italy
Messages: 46
Registered: July 2005
Karma: 0
Member
Can you explain that better?
Do I have to empty the group cache table and then either let the script run or execute a Consistency Check?
Maybe I have to register a new user and try with his credentials, and if it works manually edit old users?
Re: Adding users to a group using a script [message #30323 is a reply to message #30320] Fri, 17 February 2006 19:57 Go to previous messageGo to next message
Ilia is currently offline  Ilia   Canada
Messages: 13241
Registered: January 2002
Karma: 0
Senior Member
Administrator
Core Developer
If you empty that table, the consistency checker will rebuild it for your based on the data found within the group setting tables.

FUDforum Core Developer
Re: Adding users to a group using a script [message #30371 is a reply to message #30191] Mon, 20 February 2006 20:45 Go to previous messageGo to next message
djechelon is currently offline  djechelon   Italy
Messages: 46
Registered: July 2005
Karma: 0
Member
Here's what I tried to do now.

A user tried to access the restricted zone as described.
He was displayed in Groups Management Panel, but couldn't access restricted zone.

I went into the database and searched for his user ID.
Nothing related to that ID was found inside the group cache table.
I ran consistency checker and re-checked for user ID in the database.
I emptied the group cache table using TRUNCATE TABLE Forum_group_cache from phpMyAdmin
I ran consistency checker but still no entry in the group cache.

I went into Groups Admin, deleted that user and re-added it manually by username.
His ID was found in the group cache as finally expected, and he can now enter the forums.

I think there is nothing wrong with the query
"INSERT INTO Forum_group_members (user_id , group_id , group_members_opt) VALUES ('".$uid."', '".$gid."', '".$group_opts."');

Because I copied it from FUDforum source.
I found a way to avoid cache rebuilding by sending direct queries to the group cache table. I haven't tested it yet but it could be a good solution for MY CASE only.
But I found that even if I set permissions 312975 (that SHOULD be all permissions except edit, del, sticky, attach, break, close, move) the option field in group cache table is set to 378511. Is it normal?
But still: when I run consistency check it does actually not rebuild group cache correctly. I mean that if I empty the group cache table and run the consistency check the table is filled with old values. I think there is a problem with the checker.
Re: Adding users to a group using a script [message #30382 is a reply to message #30371] Tue, 21 February 2006 15:20 Go to previous message
Ilia is currently offline  Ilia   Canada
Messages: 13241
Registered: January 2002
Karma: 0
Senior Member
Administrator
Core Developer
I think I see the cause for your problem, when adding a user you need to make sure you set the 65536 bit. This bit indicates that this is an approved group member. Without it, the record is ignored by group cache rebuild.

FUDforum Core Developer
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Serious issue with mailing list - thread breaks when a message is modified
Next Topic: How to fix error when can't accesss admin control panel?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Sep 20 19:35:13 GMT 2024

Total time taken to generate the page: 0.02515 seconds