-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwoUser.py
More file actions
189 lines (158 loc) · 4.67 KB
/
Copy pathwoUser.py
File metadata and controls
189 lines (158 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from time import sleep
from woErrors import *
from woLimit import constrainList
from woPam import unLockUser, lockUser, isUserLocked
from woCommon import getCmdOutput
from woSystem import isAdmin, getAllNormalUsers, isNormalUser, getCurrentUsers
try:
from lxml import etree as xml
except:
import xml.etree.ElementTree as xml
#########################################################
class user(constrainList):
def __init__(self,name=None, settings=None):
constrainList.__init__(self)
self.notified = False
self.locked = False
self.name = ""
self.admin = False
if name is not None:
self.name = name
if settings is not None:
self.loadSettings(settings)
def getName(self):
return self.name
def isAdmin(self):
return self.admin
def isLocked(self):
return self.locked
def isLoggedIn(self):
return self.name in getCurrentUsers()
def isNormal(self):
return isNormalUser(self.name)
def isNotified(self):
return self.notified
def isRestricted(self):
if len(self.getAllConstrains()) == 0: return False
if not any(c.isEnabled() for c in self.getAllConstrains()): return False
return True
def loadSettings(self, e):
'''
loads the user from tag, if more tags are passed the first user is loaded
@param e: the xml tag
@return: None
'''
if e.tag == 'user':
ue = e
else:
ue = e.find(".//user")
if ue is not None:
g = ue.get("name")
self.name = g
g = ue.get("admin")
self.admin = (g == 'true')
constrainList.loadSettings(self, ue)
def loadState(self, e, force=False):
if e.tag == 'user' and e.get('name') == self.name:
ue = e
else:
ue = e.find(".//user[@name='" + self.name + "']")
if ue is not None:
g = ue.get("locked")
self.locked = (g == 'true')
constrainList.loadState(self, e, force)
def lock(self):
self.locked = True
lockUser(self.name) # wopam.py
def logout(self):
if self.isLoggedIn():
#this is a pretty bad way of killing a users processes, but we warned 'em
getCmdOutput('pkill -SIGTERM -u %s' % self.name)
sleep(5)
if self.isLoggedIn():
getCmdOutput('pkill -SIGKILL -u %s' % self.name)
def saveSettings(self):
u = xml.Element('user')
u.attrib['name'] = self.name
u.attrib['admin'] = ('true' if self.admin else 'false')
u.append(constrainList.saveSettings(self))
return u
def saveState(self):
u = xml.Element('user')
u.attrib['name'] = self.name
u.attrib['locked'] = ('true' if self.locked else 'false')
u.append(constrainList.saveState(self))
return u
def setAdmin(self, p):
self.admin = p
def setName(self, p):
self.name = p
def unlock(self):
self.locked = False
unLockUser(self.name) # wopam.py
##################################################################
class userList(object):
def __init__(self):
self.users = {}
self.index = 0
def __iter__(self):
for u in self.users.itervalues():
yield u
def __getitem__(self, item):
return self.users[item]
def __len__(self):
return len(self.users)
def __contains__(self, item):
return item in (u.getName() for u in self.users)
def addUser(self, name=None, settings=None):
'''
adds a user to the list.
@param name: user name, must be unique.
'''
if name is not None:
u=user(name=name)
elif settings is not None:
u=user(settings=settings)
else:
print "Error in addUser: specify either a nmae or xml settings"
return
if u.getName() in self.users.keys():
raise AppendError("Cannot add user, %s already exists" % u.getName())
else:
self.users[u.getName()] = u
if name is not None and settings is not None:
self.users[name].loadSettings(settings)
def createNormalUsersFromPam(self):
'''populates the user list with all normal users from the system'''
for n in getAllNormalUsers():
self.addUser(name=n)
def delAllUsers(self):
self.__init__()
def getAllUserNames(self):
'''returns all user names as list'''
ulist = self.users.keys()
return ulist
def loadSettings(self, e):
'''delete all current users and load user settings from xml etree
@param e: xml etree object
@return: None
'''
self.delAllUsers()
if e.tag == 'users':
us = e
else:
us = e.find('.//users')
if us is not None:
for u in us.findall('user'):
self.addUser(settings=u)
else:
print "Error: no users found."
return us
def saveSettings(self):
e = xml.Element('users')
for u in self:
e.append(u.saveSettings())
return e
def update(self):
for u in self:
u.update()