Blame action/user.py

Ivan Mahonin b838e2
Ivan Mahonin b838e2
Ivan Mahonin b838e2
import exception
Ivan Mahonin b838e2
from action.action import Action
Ivan Mahonin b838e2
Ivan Mahonin b838e2
Ivan Mahonin b838e2
class UserBase(Action):
Ivan Mahonin b838e2
  def parse_user_id(self, request):
Ivan Mahonin b838e2
    user_id = 0
Ivan Mahonin b838e2
    try:
Ivan Mahonin b838e2
      user_id = int(request.postvars.get('user_id', 0))
Ivan Mahonin b838e2
    except Exception:
Ivan Mahonin b838e2
      raise exception.ActionError( request.t('Used Id incorrect') )
Ivan Mahonin b838e2
    if not user_id:
Ivan Mahonin b838e2
      user_id = self.model.myrights.user_id
Ivan Mahonin b838e2
    if not user_id:
Ivan Mahonin b838e2
      raise exception.ActionError( request.t('Used Id incorrect') )
Ivan Mahonin b838e2
    return user_id
Ivan Mahonin b838e2
  
Ivan Mahonin e146e6
  def redirect_to_user(self, request, user, path = None):
Ivan Mahonin e146e6
    if path is None: path = list()
Ivan Mahonin e146e6
    return request.answer.complete_redirect(['user', str(user.login)] + path)
Ivan Mahonin e146e6
  
Ivan Mahonin b838e2
Ivan Mahonin b838e2
class UserLogin(UserBase):
Ivan Mahonin b838e2
  def __init__(self):
Ivan Mahonin b838e2
    super().__init__()
Ivan Mahonin b838e2
    self.readonly = True
Ivan Mahonin b838e2
  
Ivan Mahonin b838e2
  def process(self, request):
Ivan Mahonin b838e2
    login = str(request.postvars.get('login', ''))
Ivan Mahonin b838e2
    password = str(request.postvars.get('password', ''))
Ivan Mahonin b838e2
    if not login and not password:
Ivan Mahonin b838e2
      request.server.sessions.close_session(request)
Ivan Mahonin b838e2
    else:
Ivan Mahonin b838e2
      user_id = request.model.users.check_password(login, password)
Ivan Mahonin b838e2
      if not user_id:
Ivan Mahonin b838e2
        raise exception.ActionError( request.t('Login or password incorrect') )
Ivan Mahonin b838e2
      request.server.sessions.create_session(request, user_id)
Ivan Mahonin b838e2
    return request.answer.complete_redirect()
Ivan Mahonin b838e2
Ivan Mahonin b838e2
Ivan Mahonin b838e2
class UserCreate(UserBase):
Ivan Mahonin b838e2
  def process(self, request):
Ivan Mahonin b838e2
    login = str(request.postvars.get('login', ''))
Ivan Mahonin b838e2
    password = str(request.postvars.get('password', ''))
Ivan Mahonin b838e2
    passwordretry = str(request.postvars.get('passwordretry', ''))
Ivan Mahonin b838e2
    if password != passwordretry:
Ivan Mahonin b838e2
      raise exception.ActionError( request.t('Passwords mismatch') )
Ivan Mahonin b838e2
    name = str(request.postvars.get('name', ''))
Ivan Mahonin b838e2
Ivan Mahonin b838e2
    user = None
Ivan Mahonin b838e2
    try:
Ivan Mahonin b838e2
      user = request.model.users.create(login, password, name)
Ivan Mahonin b838e2
    except Exception as e:
Ivan Mahonin b838e2
      self.propagate_exception(e)
Ivan Mahonin b838e2
Ivan Mahonin b838e2
    request.connection.commit()
Ivan Mahonin e146e6
    return self.redirect_to_user(request, user)
Ivan Mahonin b838e2
Ivan Mahonin b838e2
Ivan Mahonin b838e2
class UserUpdate(UserBase):
Ivan Mahonin b838e2
  def process(self, request):
Ivan Mahonin b838e2
    user_id = self.parse_user_id(request)
Ivan Mahonin b838e2
    name = str(request.postvars.get('name', ''))
Ivan Mahonin b838e2
    
Ivan Mahonin b838e2
    user = request.model.users.get_by_id(user_id)
Ivan Mahonin b838e2
    if not user:
Ivan Mahonin b838e2
      raise exception.ActionError( request.t('Used not found') )
Ivan Mahonin b838e2
    
Ivan Mahonin b838e2
    try:
Ivan Mahonin b838e2
      user.update(name)
Ivan Mahonin b838e2
    except Exception as e:
Ivan Mahonin b838e2
      self.propagate_exception(e)
Ivan Mahonin b838e2
Ivan Mahonin b838e2
    request.connection.commit()
Ivan Mahonin e146e6
    return self.redirect_to_user(request, user)
Ivan Mahonin b838e2
    
Ivan Mahonin b838e2
Ivan Mahonin b838e2
class UserDelete(UserBase):
Ivan Mahonin b838e2
  def process(self, request):
Ivan Mahonin b838e2
    user_id = self.parse_user_id(request)
Ivan Mahonin b838e2
    password = request.postvars.get('password', '')
Ivan Mahonin b838e2
    if not password is None:
Ivan Mahonin b838e2
      password = str(password)
Ivan Mahonin b838e2
    
Ivan Mahonin b838e2
    user = request.model.users.get_by_id(user_id)
Ivan Mahonin b838e2
    if not user:
Ivan Mahonin b838e2
      raise exception.ActionError( request.t('Used not found') )
Ivan Mahonin b838e2
    
Ivan Mahonin b838e2
    try:
Ivan Mahonin b838e2
      user.delete(password)
Ivan Mahonin b838e2
    except Exception as e:
Ivan Mahonin b838e2
      self.propagate_exception(e)
Ivan Mahonin b838e2
Ivan Mahonin b838e2
    request.connection.commit()
Ivan Mahonin b838e2
    return request.answer.complete_redirect([])
Ivan Mahonin b838e2
    
Ivan Mahonin b838e2
Ivan Mahonin b838e2
class UserSetPassword(UserBase):
Ivan Mahonin b838e2
  def process(self, request):
Ivan Mahonin b838e2
    user_id = self.parse_user_id(request)
Ivan Mahonin b838e2
    oldpassword = request.postvars.get('oldpassword', '')
Ivan Mahonin b838e2
    newpassword = str(request.postvars.get('newpassword', ''))
Ivan Mahonin b838e2
    newpasswordretry = str(request.postvars.get('newpasswordretry', ''))
Ivan Mahonin b838e2
    if newpassword != newpasswordretry:
Ivan Mahonin b838e2
      raise exception.ActionError( request.t('Passwords mismatch') )
Ivan Mahonin b838e2
    if not oldpassword is None:
Ivan Mahonin b838e2
      oldpassword = str(oldpassword)
Ivan Mahonin b838e2
    
Ivan Mahonin b838e2
    user = request.model.users.get_by_id(user_id)
Ivan Mahonin b838e2
    if not user:
Ivan Mahonin b838e2
      raise exception.ActionError( request.t('Used not found') )
Ivan Mahonin b838e2
    
Ivan Mahonin b838e2
    try:
Ivan Mahonin b838e2
      user.change_password(newpassword, oldpassword)
Ivan Mahonin b838e2
    except Exception as e:
Ivan Mahonin b838e2
      self.propagate_exception(e)
Ivan Mahonin b838e2
Ivan Mahonin b838e2
    request.connection.commit()
Ivan Mahonin e146e6
    return self.redirect_to_user(request, user)
Ivan Mahonin b838e2
    
Ivan Mahonin b838e2
Ivan Mahonin b838e2
class UserSetSuperuser(UserBase):
Ivan Mahonin b838e2
  def process(self, request):
Ivan Mahonin b838e2
    user_id = self.parse_user_id(request)
Ivan Mahonin b838e2
    superuser = bool(request.postvars.get('superuser', False))
Ivan Mahonin b838e2
    
Ivan Mahonin b838e2
    user = request.model.users.get_by_id(user_id)
Ivan Mahonin b838e2
    if not user:
Ivan Mahonin b838e2
      raise exception.ActionError( request.t('User not found') )
Ivan Mahonin b838e2
    
Ivan Mahonin b838e2
    try:
Ivan Mahonin b838e2
      user.set_superuser(superuser)
Ivan Mahonin b838e2
    except Exception as e:
Ivan Mahonin b838e2
      self.propagate_exception(e)
Ivan Mahonin b838e2
Ivan Mahonin b838e2
    request.connection.commit()
Ivan Mahonin e146e6
    return self.redirect_to_user(request, user)
Ivan Mahonin b838e2
Ivan Mahonin b838e2
Ivan Mahonin b838e2
actions = {
Ivan Mahonin b838e2
  'login'        : UserLogin(),
Ivan Mahonin b838e2
  'create'       : UserCreate(),
Ivan Mahonin b838e2
  'update'       : UserUpdate(),
Ivan Mahonin b838e2
  'delete'       : UserDelete(),
Ivan Mahonin b838e2
  'setpassword'  : UserSetPassword(),
Ivan Mahonin b838e2
  'setsuperuser' : UserSetSuperuser(),
Ivan Mahonin b838e2
}
Ivan Mahonin b838e2