We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38925
    • 38 Posts
    Hi everyone !

    Is there a way to add an ModX user using a add-on or a snippet ?

    Ideally, I'd want to import a CSV file and add users using data in it.

    Thanks for your help.
      • 3749
      • 24,544 Posts
      Here's a utility I wrote to create a bunch of bogus users. It should get you started:

      <?php
      
      $groups = 'group1,group2,group3';
      $username = 'BogusUser';
      $password = 'password'
      $email = '[email protected]';
      
      for ($i = 1; $i <= $count; $i++) {
          $fields = array(
              'username' => $username . $i,
              'password' => $password,
              'email' => $email,
              'active' => '1',
              'blocked' => '0'
          );
          $user = $modx->newObject('modUser', $fields);
          $user->save();
          $profile = $modx->newObject('modUserProfile');
          $profile->set('email',$email);
          $user->addOne($profile);
          if ($groups) {
              $groupNames = explode(',',$groups);
              foreach ($groupNames as $groupName) {
                  if (! $modx->getObject('modUserGroup', array('name'=>$groupName))) {
                      $group = $modx->newObject('modUserGroup', array('name'=>$groupName));
                      $group->save();
                  }
                  $user->joinGroup(trim($groupName));
              }
          }
          $user->save();
          $profile->save();



      ---------------------------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
      MODX info for everyone: http://bobsguides.com/modx.html
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 38925
        • 38 Posts
        I forgot to specify that I'm with Revolution 2.2.1-dev

        Here's my code based on yours :

        $fields = array(
        				'username' => $alias,
        				'password' => $password,
        				'email' => $email,
        				'active' => 1,
        				'blocked' => 0,
        				);
        			
        			$user = $modx->newObject('modUser', $fields);
        			$user->save();
        			$userProfile = $modx->newObject('modUserProfile');
        			$userProfile->set('fullname',$prenom. ' ' .$nom);
        			$userProfile->set('email',$email);
        			$success = $user->addOne($userProfile);
        			if ($success) 
        			{
                        $user->joinGroup(trim('Administrator')); //EDIT
                        $user->save();
        				$userProfile->save();
        				mail('[email protected]', NULL, $password); //Just send password to my email during tests
            			echo '<p>Success</p>
        ';
        			} 
        			else 
        			{
            			echo '<p>Failed</p>
        ';
        			}		


        It works well but when I want to log in, it goes back to the login page, without any error message. I tried with a wrong password and got the error message.

        Is there a way to generate randomly a password with ModX ?

        EDIT : Added line 17. User apears in Administrator group but I still can't connect. [ed. note: tzoreol last edited this post 12 years, 2 months ago.]
          • 37377
          • 64 Posts
          You can use the following code to generate a random password:

          function createRandomPassword() { 
          
              $chars = "abcdefghijkmnopqrstuvwxyz023456789"; 
              srand((double)microtime()*1000000); 
              $i = 0; 
              $pass = '' ; 
          
              while ($i <= 7) { 
                  $num = rand() % 33; 
                  $tmp = substr($chars, $num, 1); 
                  $pass = $pass . $tmp; 
                  $i++; 
              } 
          
              return $pass; 
          
          } 


          You then have to encode it to md5 and save it

          $password = createRandomPassword();
          
          $user = $modx->newObject('modUser');
          	$user->fromArray(array(
          		'username' => $username,
          		'password' => md5($password)
          	));
          	
          	$user->save();
          


          Don't forget to echo your generated (uncoded) password somewhere along the process smiley
            • 38925
            • 38 Posts
            I've already generated my password but I've heard ModX can generate passwords himself, so instead of write my own function, is there one from Modx ?

            Unfortunately encoding with md5 gave me a wrong password error. [ed. note: tzoreol last edited this post 12 years, 2 months ago.]
              • 37377
              • 64 Posts
              That's strange because it has worked for me.

              It might be that you have to add a user profile as well in order to log in (haven't tested it yet)

              You can do this as follows:

              
              $uid = $user->get('id');
              $userProfile = $modx->newObject('modUserProfile');
              		$userProfile->fromArray(array(
              			'internalKey' => $uid,
              			'fullname' => $fullname,
              			'email' => $email,
              			'phone' => $phone,
              			'address' => $address,
              			'city' => $city,
              			'zip' => $zip
              		));
              		$userProfile->save();
                • 38925
                • 38 Posts
                No changes but I noticed that number of login is incremented and Role is member whereas my admin is a super user.

                How can I attribute role to an user or change a role rights ?

                EDIT : I changed my created user to super user and it works so I just need to know how to attribute a role and how manage rights on it. Thanks! [ed. note: tzoreol last edited this post 12 years, 2 months ago.]
                  • 37377
                  • 64 Posts

                  $memberGroup = $modx->newObject('modUserGroupMember');
                  		$memberGroup->fromArray(array(
                  			'user_group' => $user_group,
                  			'member' => $uid,
                  			'role' => $role_id
                  		));
                  		$memberGroup->save();
                    • 38925
                    • 38 Posts
                    I tried this :

                    $uid = $user->get('id');
                    			$userProfile = $modx->newObject('modUserProfile');
                    			$userProfile->fromArray(array(
                    				'internalKey' => $uid,
                    				'fullname' => $prenom . $nom,
                    				'email' => $email,
                    				'address' => '1 Baker Street',
                    				'city' => 'London',
                    				'zip' => '30777',
                    				));
                    			$memberGroup = $modx->newObject('modUserGroupMember');
                    			$memberGroup->fromArray(array(
                    				'user_group' => 'Administrator',
                    				'member' => $uid,
                    				'role' => 2,
                    				));
                    			$memberGroup->save();
                    			$success = $user->addOne($userProfile);
                    			if ($success) 
                    			{
                    				$user->joinGroup(trim('Administrator'));
                        			$user->save();
                    				$userProfile->save();
                    				mail('[email protected]', NULL, 'Login : ' .$alias. '\nMot de passe : '  .$password);//Send password just for test
                        			echo '<p>Success</p><br/>';
                    			} 
                    			else 
                    			{
                        			echo '<p>Fail</p><br/>';
                    			}		


                    But role still to member (id = 1) [ed. note: tzoreol last edited this post 12 years, 2 months ago.]
                      • 37377
                      • 64 Posts
                      The user_group has to be an id from the modx_membergroup_names table

                      And you have to do
                      $user->save();
                      before you can add the user's profile and roles because otherwise you don't have the id of the user to relate to

                      $uid = $user->get('id');

                      You can only use this after the user is added