Overview

Classes

  • Package
  • Service
  • Tlaloc_File
  • User
  • Overview
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * A User management object meant for OpenBSD
  4:  *
  5:  * this object should allow; listing, verifying, updating, removing and adding
  6:  * users of all sort under OpenBSD.
  7:  * Meant to virtualize the base command line tools so we can later port
  8:  * this object to other OSes like Linux, CentOS, etc..
  9:  *
 10:  */
 11: class User {
 12:     public $SUPPORTED_COMMANDS=array("exists"=>"Check to see if a user exists","create"=>"Create a new user","delete"=>"Delete a local user",
 13:                         "test"=>"Test the creation of a user foo");
 14:     public $Repos = array();
 15: 
 16:     /**
 17:      * User::__construct()
 18:      * Instantiate a User object, no parameters necessary
 19:      */
 20:     function __construct(){
 21: 
 22:     }
 23: 
 24: 
 25:     /**
 26:      * User::Exists()
 27:      * Verify the existence of a particular user
 28:      * @param mixed $Username
 29:      * @return boolean $result
 30:      */
 31:     public function Exists($Username){
 32:         $result=`grep "^$Username:" /etc/passwd | wc -l`;
 33:         if (trim($result)=="1") {
 34:             return true;
 35:         } else {
 36:             return false;
 37:         }
 38:     }
 39: 
 40:     /**
 41:      * User::GroupExists()
 42:      * Verify the existence of a particular group
 43:      * @param mixed $Groupname
 44:      * @return boolean $result
 45:      */
 46:     public function GroupExists($Groupname){
 47:         //print "Checking for the existence of $Username\n";
 48:         $result=`grep "^$Groupname:" /etc/group | wc -l`;
 49:         //print "result = $result\n";
 50:         if (trim($result)=="1") {
 51:             return true;
 52:         } else {
 53:             return false;
 54:         }
 55:     }
 56: 
 57:     /**
 58:      * User::Create()
 59:      * Create a Unix User.
 60:      * @param mixed $Username
 61:      * @param mixed $Password
 62:      * @param mixed $RealName
 63:      * @param mixed $target_UID
 64:      * @param mixed $LoginGroup
 65:      * @param mixed $LoginClass
 66:      * @param mixed $HomeFolder
 67:      * @param mixed $ShellToUse
 68:      * @return boolean $result
 69:      */
 70:     public function Create($Username,$Password=null,$RealName=null,$target_UID=null,$LoginGroup=null,$LoginClass=null,$HomeFolder=null,$ShellToUse=null){
 71:         global $LOGLEVEL;
 72: 
 73:         if (! is_null($LoginGroup)) {
 74:             $group = "-g $LoginGroup";
 75:         } else {
 76:             $group = "-g =uid";
 77:         }
 78:         if (! is_null($LoginClass)) {
 79:             $loginclass = "-L $LoginClass";
 80:         } else {
 81:             $loginclass = "";
 82:         }
 83:         if (! is_null($RealName)) {
 84:             $comment = "-c \"$RealName\"";
 85:         } else {
 86:             $comment = "''";
 87:         }
 88:         if (! is_null($HomeFolder)) {
 89:             $homefolder = "-d $HomeFolder ";
 90:             if (! file_exists($HomeFolder)) {
 91:                 $createhomefolder = "-m";
 92:             } else {
 93:                 $createhomefolder="";
 94:             }
 95: 
 96:         } else {
 97:             $createhomefolder="";
 98:             $homefolder = "";
 99:         }
100: 
101:         if (! is_null($ShellToUse)) {
102:             $shell = "-s $ShellToUse";
103:         } else {
104:             $shell = "";
105:         }
106: 
107:         if (! is_null($Password)) {
108:             // need to encrypt this...
109:             $pass = "-p '$Password'";
110:         } else {
111:             $pass = "";
112:         }
113:         if (! is_null($target_UID)) {
114:             $uid_min = "-u $target_UID";
115:         } else {
116:             $uid_min = "";
117:         }
118: 
119:         /**
120:          * create the User
121:          *
122:          */
123:         if (! $this->Exists($Username) ) {
124:             if ($LOGLEVEL > 0) {
125:                 print "Executing :\nuseradd $createhomefolder $shell $group $uid_min $homefolder $pass $comment $Username\n";
126:             }
127:             $result=`useradd $createhomefolder $shell $group $loginclass $uid_min $homefolder $pass $comment $Username`;
128:             return $this->Exists($Username);
129:         } else {
130:             return true;
131:         }
132:     }
133: 
134:     /**
135:      * User::User_In_Group()
136:      * Verify if a User is part of a Group
137:      * @param mixed $User
138:      * @param mixed $Group
139:      * @return boolean result
140:      */
141:     public function User_In_Group($User,$Group){
142:         $result=`grep "^$Groupname:" /etc/group | grep "$User" | wc -l`;
143:         //print "result = $result\n";
144:         if (trim($result)=="1") {
145:             return true;
146:         } else {
147:             return false;
148:         }
149:     }
150: 
151:     /**
152:      * User::AddToGroup()
153:      * Add a User to a Unix Group
154:      * @param mixed $User
155:      * @param mixed $SecondaryGroups
156:      * @return boolean result
157:      */
158:     public function AddToGroup($User,$SecondaryGroups){
159:         if ( $this->Exists($User) ) {
160:             if (! $this->User_In_Group($User,$SecondaryGroups)) {
161:                 print "Executing :\nAddToGroup -G $SecondaryGroups $User\n";
162:                 $result=`usermod -G $SecondaryGroups $User`;
163:                 return $result;
164:             } else {
165:                 return true;
166:             }
167: 
168:         } else {
169:             return true;
170:         }
171:     }
172: 
173: 
174:     /**
175:      * User::CreateGroup()
176:      * Create a Unix Group
177:      * @param mixed $Group_Name
178:      * @param mixed $GID
179:      * @return boolean result
180:      */
181:     public function CreateGroup($Group_Name,$GID=null){
182: 
183:         if (! $this->GroupExists($Group_Name) ) {
184:             if (! is_null($GID)) {
185:                 $gid_str="-g $GID";
186:             } else {
187:                 $gid_str="";
188:             }
189:             $result=`groupadd $gid_str $Group_Name`;
190:             return $this->GroupExists($Group_Name);
191: 
192:         } else {
193:             return true;
194:         }
195:     }
196: 
197:     /**
198:      * User::Delete()
199:      * Delete a Unix User
200:      * @param mixed $Username
201:      * @return boolean result
202:      */
203:     public function Delete($Username){
204:         if ( $this->Exists($Username) ) {
205:             /**
206:              * On OpenBSD, rmuser is more thorough than userdel
207:              *
208:              */
209: 
210:             $result=`userdel -r $Username`;
211:             if ($this->Exists($Username)) {
212:                 return false;
213:             } else {
214:                 return true;
215:             }
216:         } else {
217:             return true;
218:         }
219:     }
220: }
221: 
222: ?>
223: 
API documentation generated by ApiGen 2.6.0