20 from xml.etree
import ElementTree
38 TOSTR = {NONE :
'none',
39 OPERATOR :
'operator',
40 MAINTENANCE:
'maintenance',
42 DEVELOPER :
'developer'}
44 TOLEVEL = {
'none' : NONE,
45 'operator' : OPERATOR,
46 'maintenance': MAINTENANCE,
48 'developer' : DEVELOPER}
55 def __init__(self, userid = None, privilege = Privilege.NONE):
65 if userid
is not None:
68 def init(self, userid, privilege):
76 self.
password = base64.b64encode(password)
85 return base64.b64decode(self.
password)
95 """! The constructor."""
96 QObject.__init__(self)
101 """! Set user informations and call at callbacks the new connection.
102 @param userinfo: user connected information.
103 @type userinfo: User.
106 self.emit(SIGNAL(
'userAccountChanged'), self.
_user)
109 """! Get current user informations.
110 @return userinfo: user informations.
111 @type userinfo: User.
122 xmlstr =
"""<?xml version='1.0' encoding='utf8'?>
125 <created>22-05-2014</created>
126 <modified>23-05-2014</modified>
127 <privilege>developer</privilege>
128 <password>YXRpMDA2</password>
131 <created>22-05-2014</created>
132 <modified>23-05-2014</modified>
133 <privilege>operator</privilege>
134 <password>YXRpMDA2</password>
138 xmlencode = base64.encodestring(xmlstr)
140 with open(os.path.join(db_dir),
'w')
as f_db:
141 f_db.write(xmlencode)
147 xmlstr =
"""<?xml version='1.0' encoding='utf8'?>
151 xmlencode = base64.encodestring(xmlstr)
153 with open(os.path.join(db_dir),
'w')
as f_db:
154 f_db.write(xmlencode)
161 if not elem.text
or not elem.text.strip():
163 if not elem.tail
or not elem.tail.strip():
167 if not elem.tail
or not elem.tail.strip():
170 if level
and (
not elem.tail
or not elem.tail.strip()):
173 setattr(ElementTree,
'indent', indent)
184 """ Manage user account file xml:
188 - Modif user account,
189 - Remove user account.
192 ACCOUNTS_FILENAME =
'accounts.db'
196 PRIVILEGE =
'privilege'
197 PASSWORD =
'password'
201 MODIFIED =
'modified'
203 USER_STR_ITEM =
"""<user id="%s">
204 <created>%s</created>
205 <modified>%s</modified>
206 <privilege>%s</privilege>
207 <password>%s</password>
217 accounts_encode = file_encode.read()
220 accounts_decode = base64.decodestring(accounts_encode)
221 except Exception
as e:
222 raise CobotGuiException(
'The user accounts file is corrupted "%s"!'%str(e))
227 self.
accounts_xml = ElementTree.fromstring(accounts_decode)
228 except Exception
as e:
229 raise CobotGuiException(
'UserAccountsManager.__init__() raised with exception "%s"'%e)
240 xmlencode = base64.encodestring(xmlstr)
244 f_accounts.write(xmlencode)
246 from shutil
import copyfile
249 os.path.join(self.
accounts_dir,
'backup',
'accounts_back.db'))
258 with open(os.path.join(self.
accounts_dir,
'accounts.xml'),
'w')
as \
260 f_accounts_xml.write(xmlstr)
263 return './%s[@%s="%s"]'%(self.
USER, self.
UID, userid)
266 """Read and get user(s) id list registered in user accounts file
267 @return: user_list: user(s) id list.
268 @type user_list: array string.
273 user_list.append(user.attrib[self.
UID])
278 """Read and get user account information
280 @param: userid: user id.
283 @return: userinfo: user informations.
284 @type userinfo: C{User}.
287 user_account = self.accounts_xml.find(self.
resolve_path(userid))
289 if user_account
is None:
290 rospy.logerr(
'User "%s" not found !'%userid)
294 userinfo.userid = userid
295 userinfo.created = user_account.find(self.
CREATED).text
296 userinfo.modified = user_account.find(self.
MODIFIED).text
297 userinfo.privilege = Privilege.TOLEVEL[user_account.find(self.
PRIVILEGE).text]
298 userinfo.password = user_account.find(self.
PASSWORD).text
299 userinfo.encoded =
True
304 """Add new user account in "accounts.db" file.
306 @param: userinfo: user informations.
307 @type userinfo: C{User}.
310 user_account = self.accounts_xml.find(self.
resolve_path(userinfo.userid))
312 if user_account
is not None:
313 raise CobotGuiException(
'Do not add the user id "%s" is already used !'
317 str(rospy.get_rostime()),
318 str(rospy.get_rostime()),
319 Privilege.TOSTR[userinfo.privilege],
324 user_xml = ElementTree.fromstring(user_str)
325 self.accounts_xml.append(user_xml)
329 except Exception
as e:
330 raise CobotGuiException(
'Do not add the user id "%s" because %s !'
331 %(userinfo.userid, str(e)))
333 def modif(self, usersource, usermodifed):
334 """Update user informations.
336 @param: usersource: current user informations.
337 @type usersource: C{User}.
339 @param: usermodifed: new user informations.
340 @type usermodifed: C{User}.
344 if usersource.userid != usermodifed.userid:
345 raise CobotGuiException(
"Change user id not allowed !")
347 user_account = self.accounts_xml.find(self.
resolve_path(usersource.userid))
349 if user_account
is None:
350 raise CobotGuiException(
'Invalid user id "%s" is not found !'
353 if usersource.password != user_account.find(self.
PASSWORD).text:
354 raise CobotGuiException(
'Invalid password from user id "%s" !'
357 user_account.find(self.
MODIFIED).text = str(rospy.get_rostime())
358 user_account.find(self.
PASSWORD).text = usermodifed.password
359 user_account.find(self.
PRIVILEGE).text = Privilege.TOSTR[usermodifed.privilege]
363 except Exception
as e:
364 raise CobotGuiException(str(e))
367 """Remove user account.
369 @param: userinfo: user informations.
370 @type userinfo: C{User}.
373 user_account = self.accounts_xml.find(self.
resolve_path(userinfo.userid))
376 self.accounts_xml.remove(user_account)
378 raise CobotGuiException(
'Connot removed user id "%s" is not registered !'
382 except Exception
as e:
383 raise CobotGuiException(str(e))
385 if __name__ ==
'__main__':
387 print Privilege.LEVEL[Privilege.NONE]
388 print Privilege.LEVEL[Privilege.OPERATOR]
389 print Privilege.LEVEL[Privilege.MAINTENANCE]
390 print Privilege.LEVEL[Privilege.EXPERT]
391 print Privilege.LEVEL[Privilege.DEVELOPER]
def get_current_user_account
Get current user informations.
tuple airbus_cobot_gui_dir
def __init__
The constructor.
Manage user accounts file.
def update
Set user informations and call at callbacks the new connection.
Class for difine different levels of user access.