Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 import rospy
00020 import os
00021 import base64
00022 from xml.etree import ElementTree
00023 from python_qt_binding.QtCore import *
00024
00025 from user import User
00026 from privilege import Privilege
00027
00028 from airbus_cobot_gui.util import CobotGuiException
00029 from airbus_cobot_gui.res import R
00030
00031 def xml_to_db_file():
00032
00033 xmlstr = """<?xml version='1.0' encoding='utf8'?>
00034 <accounts>
00035 <user id="mmn">
00036 <created>22-05-2014</created>
00037 <modified>23-05-2014</modified>
00038 <privilege>developer</privilege>
00039 <password>YXRpMDA2</password>
00040 </user>
00041 <user id="martin">
00042 <created>22-05-2014</created>
00043 <modified>23-05-2014</modified>
00044 <privilege>operator</privilege>
00045 <password>YXRpMDA2</password>
00046 </user>
00047 </accounts>"""
00048
00049 xmlencode = base64.encodestring(xmlstr)
00050
00051 with open(R.accounts.dir+'/accounts_init.db','w') as f_db:
00052 f_db.write(xmlencode)
00053
00054 def root_xml_to_db_file():
00055
00056 xmlstr = """<?xml version='1.0' encoding='utf8'?>
00057 <accounts>
00058 </accounts>"""
00059
00060 xmlencode = base64.encodestring(xmlstr)
00061
00062 with open(R.accounts.dir+'/accounts_root.db','w') as f_db:
00063 f_db.write(xmlencode)
00064
00065
00066 def indent(elem, level=0):
00067
00068 i = "\n" + level*" "
00069 if len(elem):
00070 if not elem.text or not elem.text.strip():
00071 elem.text = i + " "
00072 if not elem.tail or not elem.tail.strip():
00073 elem.tail = i
00074 for elem in elem:
00075 indent(elem, level+1)
00076 if not elem.tail or not elem.tail.strip():
00077 elem.tail = i
00078 else:
00079 if level and (not elem.tail or not elem.tail.strip()):
00080 elem.tail = i
00081
00082 setattr(ElementTree, 'indent', indent)
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 class UserAccounts:
00093 """ Manage user account file xml:
00094 - Get user list
00095 - Find user account,
00096 - Add user account,
00097 - Modif user account,
00098 - Remove user account.
00099 """
00100
00101 ACCOUNTS_FILENAME = 'accounts.db'
00102
00103
00104 USER = 'user'
00105 PRIVILEGE = 'privilege'
00106 PASSWORD = 'password'
00107
00108 UID = 'id'
00109 CREATED = 'created'
00110 MODIFIED = 'modified'
00111
00112 USER_STR_ITEM = """<user id="%s">
00113 <created>%s</created>
00114 <modified>%s</modified>
00115 <privilege>%s</privilege>
00116 <password>%s</password>
00117 </user>"""
00118
00119 def __init__(self, context=None):
00120 """Constructor"""
00121
00122 self._context = context
00123
00124 self.accounts_dir = R.accounts.dir
00125
00126 with open(os.path.join(self.accounts_dir,self.ACCOUNTS_FILENAME),'r') as \
00127 file_encode:
00128 accounts_encode = file_encode.read()
00129
00130 try:
00131 accounts_decode = base64.decodestring(accounts_encode)
00132 except Exception as e:
00133 raise CobotGuiException('The user accounts file is corrupted "%s"!'%str(e))
00134
00135 self.accounts_xml = None
00136
00137 try:
00138 self.accounts_xml = ElementTree.fromstring(accounts_decode)
00139 except Exception as e:
00140 raise CobotGuiException('UserAccountsManager.__init__() raised with exception "%s"'%e)
00141 finally:
00142 self._xml_file_generator()
00143
00144
00145 def update(self):
00146
00147 xmlstr = ElementTree.tostring(self.accounts_xml,
00148 encoding='utf8',
00149 method='xml')
00150
00151 xmlencode = base64.encodestring(xmlstr)
00152
00153 with open(os.path.join(self.accounts_dir,self.ACCOUNTS_FILENAME),'w') as \
00154 f_accounts:
00155 f_accounts.write(xmlencode)
00156
00157 from shutil import copyfile
00158
00159 copyfile(os.path.join(self.accounts_dir,self.ACCOUNTS_FILENAME),
00160 os.path.join(self.accounts_dir,'backup','accounts_back.db'))
00161
00162 self._xml_file_generator()
00163
00164 def _xml_file_generator(self):
00165
00166 xmlstr = ElementTree.tostring(self.accounts_xml,
00167 encoding='utf8',
00168 method='xml')
00169 with open(os.path.join(self.accounts_dir,'accounts.xml'),'w') as \
00170 f_accounts_xml:
00171 f_accounts_xml.write(xmlstr)
00172
00173 def resolve_path(self, userid):
00174 return './%s[@%s="%s"]'%(self.USER, self.UID, userid)
00175
00176 def user_list(self):
00177 """Read and get user(s) id list registered in user accounts file
00178 @return: user_list: user(s) id list.
00179 @type user_list: array string.
00180 """
00181 user_list = []
00182
00183 for user in self.accounts_xml:
00184 user_list.append(user.attrib[self.UID])
00185
00186 return user_list
00187
00188 def find(self, userid):
00189 """Read and get user account information
00190
00191 @param: userid: user id.
00192 @type userid: str.
00193
00194 @return: userinfo: user informations.
00195 @type userinfo: C{User}.
00196 """
00197
00198 user_account = self.accounts_xml.find(self.resolve_path(userid))
00199
00200 if user_account is None:
00201 rospy.logerr('User "%s" not found !'%userid)
00202 return None
00203
00204 userinfo = User()
00205 userinfo.userid = userid
00206 userinfo.created = user_account.find(self.CREATED).text
00207 userinfo.modified = user_account.find(self.MODIFIED).text
00208 userinfo.privilege = Privilege.TOLEVEL[user_account.find(self.PRIVILEGE).text]
00209 userinfo.password = user_account.find(self.PASSWORD).text
00210 userinfo.encoded = True
00211
00212 return userinfo
00213
00214 def add(self, userinfo):
00215 """Add new user account in "accounts.db" file.
00216
00217 @param: userinfo: user informations.
00218 @type userinfo: C{User}.
00219 """
00220
00221 user_account = self.accounts_xml.find(self.resolve_path(userinfo.userid))
00222
00223 if user_account is not None:
00224 raise CobotGuiException('Do not add the user id "%s" is already used !'
00225 %userinfo.userid)
00226
00227 user_str = self.USER_STR_ITEM%(userinfo.userid,
00228 str(rospy.get_rostime()),
00229 str(rospy.get_rostime()),
00230 Privilege.TOSTR[userinfo.privilege],
00231 userinfo.password)
00232
00233 try:
00234
00235 user_xml = ElementTree.fromstring(user_str)
00236 self.accounts_xml.append(user_xml)
00237 ElementTree.indent(self.accounts_xml)
00238 self.update()
00239
00240 except Exception as e:
00241 raise CobotGuiException('Do not add the user id "%s" because %s !'
00242 %(userinfo.userid, str(e)))
00243
00244 def modif(self, usersource, usermodifed):
00245 """Update user informations.
00246
00247 @param: usersource: current user informations.
00248 @type usersource: C{User}.
00249
00250 @param: usermodifed: new user informations.
00251 @type usermodifed: C{User}.
00252
00253 """
00254
00255 if usersource.userid != usermodifed.userid:
00256 raise CobotGuiException("Change user id not allowed !")
00257
00258 user_account = self.accounts_xml.find(self.resolve_path(usersource.userid))
00259
00260 if user_account is None:
00261 raise CobotGuiException('Invalid user id "%s" is not found !'
00262 %usersource.userid)
00263
00264 if usersource.password != user_account.find(self.PASSWORD).text:
00265 raise CobotGuiException('Invalid password from user id "%s" !'
00266 %usersource.userid)
00267 else:
00268 user_account.find(self.MODIFIED).text = str(rospy.get_rostime())
00269 user_account.find(self.PASSWORD).text = usermodifed.password
00270 user_account.find(self.PRIVILEGE).text = Privilege.TOSTR[usermodifed.privilege]
00271
00272 try:
00273 self.update()
00274 except Exception as e:
00275 raise CobotGuiException(str(e))
00276
00277 def remove(self, userinfo):
00278 """Remove user account.
00279
00280 @param: userinfo: user informations.
00281 @type userinfo: C{User}.
00282 """
00283
00284 user_account = self.accounts_xml.find(self.resolve_path(userinfo.userid))
00285
00286 try:
00287 self.accounts_xml.remove(user_account)
00288 except:
00289 raise CobotGuiException('Connot removed user id "%s" is not registered !'
00290 %userinfo.userid)
00291 try:
00292 self.update()
00293 except Exception as e:
00294 raise CobotGuiException(str(e))
00295
00296 if __name__ == '__main__':
00297
00298 print Privilege.LEVEL[Privilege.NONE]
00299 print Privilege.LEVEL[Privilege.OPERATOR]
00300 print Privilege.LEVEL[Privilege.MAINTENANCE]
00301 print Privilege.LEVEL[Privilege.EXPERT]
00302 print Privilege.LEVEL[Privilege.DEVELOPER]
00303
00304
00305