We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5338
    • 35 Posts
    How can I add a user to a webrole, programmatically?

    In other words, I would like that if a user belonging to ’Registered Users’ insert a code, he/she can be admitted in another group (e. g. ’Paying Users’).

    Which are API calls to add a new role to an existing user?
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      That’ll need a snippet or a plugin. Web users don’t have roles, but they can belong to web groups. What you need is a snippet or a plugin that adds the user to whatever web groups you want.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 7923
        • 4,213 Posts
        I don’t think that there is an api for that currently.. but here’s how it’s done:
        This is partly copied from WebSignup snippet and some of it was written to this post, so there might be typos as I didn’t test it..
        <?php
        # setup key
        $key = 0;
        if (isset($username)) {
            $sql = "SELECT id FROM ".$modx->getFullTableName("web_users")." WHERE username='$username'";
            $rs = $modx->db->query($sql);
            if(!$rs){
                return "Could not find username: $username";
            } else {
                $userinfo = $modx->fetchRow($rs);
                $key = $userinfo['id'];
            }
        } else {
            $key = $_SESSION['webInternalKey'];
        }
        # setup web groups
        $groups = isset($groups) ? explode(',',$groups) : array();
        for($i=0;$i<count($groups);$i++) $groups[$i] = trim($groups[$i]);
        
        // add user to web groups
            if(count($groups)>0 && $key != 0) {
                $ds = $modx->db->query("SELECT id FROM ".$modx->getFullTableName("webgroup_names")." WHERE name IN ('".implode("','",$groups)."')");
                if(!$ds) return "Could not find some of the web groups";
                else {
                    while ($row = $modx->fetchRow($ds)) {
                        $wg = $row["id"];
                        $modx->db->query("REPLACE INTO ".$modx->getFullTableName("web_groups")." (webgroup,webuser) VALUES('$wg','$key')");
                    }
                }
            }
        ?>
        


        And you would call that using [[YourSnippetName? &groups=`thegroups,toset,theuser,to` &username=`someusername`]], or if you don’t specify the username, it would change the groups of the logged in web user..

        But ofcourse the snippet isn’t any good like that, you would need to do maybe a button to activate it or include that code into some other snippet or a plugin.. or whatever.


          "He can have a lollipop any time he wants to. That's what it means to be a programmer."
          • 5338
          • 35 Posts
          thank you very much. That’s really what I needed.

          Is there any documentation about modx data schemes, anywhere?
            • 7923
            • 4,213 Posts
            I don’t think so.. but the data scheme will change for 1.0 and at that point there will be documentation for it..


              "He can have a lollipop any time he wants to. That's what it means to be a programmer."
              • 5338
              • 35 Posts
              Is there any roadmap for 1.0?