1: <?php
2: 3: 4: 5: 6: 7: 8: 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: 18: 19:
20: function __construct(){
21:
22: }
23:
24:
25: 26: 27: 28: 29: 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: 42: 43: 44: 45:
46: public function GroupExists($Groupname){
47:
48: $result=`grep "^$Groupname:" /etc/group | wc -l`;
49:
50: if (trim($result)=="1") {
51: return true;
52: } else {
53: return false;
54: }
55: }
56:
57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 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:
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: 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: 136: 137: 138: 139: 140:
141: public function User_In_Group($User,$Group){
142: $result=`grep "^$Groupname:" /etc/group | grep "$User" | wc -l`;
143:
144: if (trim($result)=="1") {
145: return true;
146: } else {
147: return false;
148: }
149: }
150:
151: 152: 153: 154: 155: 156: 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: 176: 177: 178: 179: 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: 199: 200: 201: 202:
203: public function Delete($Username){
204: if ( $this->Exists($Username) ) {
205: 206: 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: