Blame action/user.py

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