def create_guestuser(self): guest = User() guest.username = '******' guest.first_name = 'Guest' guest.last_name = 'User' guest.set_password('guest') guest.is_staff = True guest.save() # add permissions for table in ['session', 'collection', 'userprofile', 'deviceprofile']: for code in ['add', 'change', 'delete']: code_name = code + '_' + table permission = Permission.objects.get(codename=code_name) guest.user_permissions.add(permission) # create default model profile = UserProfile(user=guest, module='ietf-interfaces@2013-12-23') profile.save() # add default collection col = Collection(name='default', user=guest, description='Default Collection') col.save()
def _reload_cxml(self, username): user = User.objects.get(username=username) # delete all UserProfiles associated with this user user.userprofile_set.all().delete() # create new UserProfiles from CXML files for f in os.listdir(os.path.join(settings.BASE_DIR, 'data', 'users', username, 'cxml')): f_split = os.path.splitext(f) if f_split[1] == '.xml': profile = UserProfile(user=user, module=f_split[0]) profile.save()
def create_guestuser(self, cxmldir): guest = User() guest.username = '******' guest.first_name = 'Guest' guest.last_name = 'User' guest.set_password('guest') guest.is_staff = True guest.save() # add permissions for table in ['session', 'collection', 'userprofile', 'deviceprofile']: for code in ['add', 'change', 'delete']: code_name = code + '_' + table permission = Permission.objects.get(codename=code_name) guest.user_permissions.add(permission) # create list of model if cxmldir: for f in os.listdir(cxmldir): f_split = os.path.splitext(f) if f_split[1] == '.xml': profile = UserProfile(user=guest, module=os.path.basename(f_split[0])) profile.save() else: profile = UserProfile(user=guest, module='ietf-interfaces@2013-12-23') profile.save() # add default collection col = Collection(name='default', user=guest, description='Default Collection') col.save()
def admin_action(username, payload, request): if payload is None: logging.debug('Invalid request: %s for user %s' % (request, username)) return False modified = False modules = ET.fromstring(payload) user = User.objects.filter(username=username) for module in modules: name = module.text.split('.yang')[0] # delete modules from user profile if request in ['delete', 'unsubscribe']: if UserProfile.objects.filter(user=user[0], module=name).exists(): profile = UserProfile.objects.filter(user=user[0], module=name) profile.delete() logging.debug('Module %s deleted for user %s' % (module.text, username)) # delete yang and cxml files for delete request if request == 'delete': for _type in [('cxml', '.xml'), ('yang', '.yang')]: _file = os.path.join('data', 'users', username, _type[0], name + _type[1]) if os.path.exists(_file): os.remove(_file) modified = True logging.debug('Deleted %s (user: %s)' % (_file, username)) if request == 'subscribe': if not UserProfile.objects.filter(user=user[0], module=name).exists(): profile = UserProfile(user=user[0], module=name) profile.save() logging.debug('User %s subscribed to %s module ..' % (username, module.text)) else: logging.debug('User %s already subscribed to %s module ' % (username, module.text)) # if any yang model modified, delete dependency file if modified: _file = os.path.join('data', 'users', username, 'dependencies.xml') if os.path.exists(_file): os.remove(_file) logging.debug('Deleted dependency file %s (user: %s)' % (_file, username)) return True
def admin_action(username, payload, request): logger.info("ModuleAdmin.admin_action: enter (%s -> %s)" % (username, request)) if payload is None: logger.error('ModuleAdmin.admin_action: invalid payload in request !!') return False, "Invalid payload !!" modified = False modules = ET.fromstring(payload) if request == 'graph': return dependencies_graph(username, modules) users = User.objects.filter(username=username) if not users: logger.error("ModuleAdmin.admin_action: invalid user " + username) return False, 'Unknown User %s !!' % username user = users[0] if not ServerSettings.user_aware(): if (request == 'delete') and not user.has_perm('explorer.delete_yangmodel'): return False, 'User %s does not have permission to delete models!!' % username for module in modules: name = module.text.split('.yang')[0] logger.debug("ModuleAdmin.admin_action: %s -> %s" % (request, name)) # delete modules from user profile if request in ['delete', 'unsubscribe']: if UserProfile.objects.filter(user=user, module=name).exists(): profile = UserProfile.objects.filter(user=user, module=name) profile.delete() logger.debug('Module %s deleted for user %s' % (module.text, username)) # delete yang and cxml files for delete request if request == 'delete': for _type in [('cxml', '.xml'), ('yang', '.yang')]: _file = os.path.join('data', 'users', username, _type[0], name + _type[1]) if os.path.exists(_file): os.remove(_file) modified = True logging.debug('Deleted %s (user: %s)' % (_file, username)) if request == 'subscribe': if not is_browsable(username, name): logger.debug('Module %s can not be subscribed ' % (module.text)) continue if not UserProfile.objects.filter(user=user, module=name).exists(): profile = UserProfile(user=user, module=name) profile.save() logger.debug('User %s subscribed to %s module ..' % (username, module.text)) else: logger.debug('User %s already subscribed to %s module ' % (username, module.text)) # if any yang model modified, delete dependency file if modified: _file = os.path.join(ServerSettings.yang_path(username), 'dependencies.xml') if os.path.exists(_file): os.remove(_file) logger.debug('Deleted dependency file %s (user: %s)' % (_file, username)) return True, None
def admin_action(username, payload, request): logging.debug("ModuleAdmin.admin_action: enter (%s -> %s)" % (username, request)) if payload is None: logging.debug('ModuleAdmin.admin_action: Invalid payload in request !!') return (False, "Invalid payload !!") modified = False modules = ET.fromstring(payload) if request == 'graph': return dependencies_graph(username, modules) users = User.objects.filter(username=username) if not users: logging.debug("ModuleAdmin.admin_action: Inavlid user " + username) return (False, 'Unknown User %s !!' % username) user = users[0] if not ServerSettings.user_aware(): if (request == 'delete') and not user.has_perm('explorer.delete_yangmodel'): return (False, 'User %s does not have permission to delete models!!' % username) for module in modules: name = module.text.split('.yang')[0] logging.debug("ModuleAdmin.admin_action: %s -> %s" % (request, name)) # delete modules from user profile if request in ['delete', 'unsubscribe']: if UserProfile.objects.filter(user=user, module=name).exists(): profile = UserProfile.objects.filter(user=user, module=name) profile.delete() logging.debug('Module %s deleted for user %s' % (module.text, username)) # delete yang and cxml files for delete request if request == 'delete': for _type in [('cxml', '.xml'), ('yang', '.yang')]: _file = os.path.join('data', 'users', username, _type[0], name + _type[1]) if os.path.exists(_file): os.remove(_file) modified = True logging.debug('Deleted %s (user: %s)' % (_file, username)) if request == 'subscribe': if not is_browsable(username, name): logging.debug('Module %s can not be subscribed ' % (module.text)) return (False, 'Module %s can not be subscribed, not a main module !!' % name) if not UserProfile.objects.filter(user=user, module=name).exists(): profile = UserProfile(user=user, module=name) profile.save() logging.debug('User %s subscribed to %s module ..' % (username, module.text)) else: logging.debug('User %s already subscribed to %s module ' % (username, module.text)) # if any yang model modified, delete dependency file if modified: _file = os.path.join(ServerSettings.yang_path(username), 'dependencies.xml') if os.path.exists(_file): os.remove(_file) logging.debug('Deleted dependency file %s (user: %s)' % (_file, username)) return (True, None)