Exemplo n.º 1
0
def validateRegister(userInfo):
    if not userInfo:
        return "Invalid user information."
    else:
        msg = validateEmail(userInfo.email)
        if not stringEx.isNullOrWhitespace(msg):
            return msg
        msg = validateNickName(userInfo.nickName)
        if not stringEx.isNullOrWhitespace(msg):
            return msg
        msg = validatePassword(userInfo.pwd, userInfo.cfmPwd)
        if not stringEx.isNullOrWhitespace(msg):
            return msg
        return None
Exemplo n.º 2
0
def logout(request):
    if request.method == 'POST':
        session_id = request.COOKIES.get('session')
        if session_id is not None and (
                not stringEx.isNullOrWhitespace(session_id)):
            session.removeSessionBySessionid(session_id)
    return render(request, 'DeanSpace/home.html')
Exemplo n.º 3
0
def getAuth(request):
    if request is not None and request.method == 'GET':
        session_id = request.COOKIES.get('session')
        if session_id is not None and (
                not stringEx.isNullOrWhitespace(session_id)):
            auth = Auth()
            auth.setAuthBySessionId(session_id)
            return auth
    return None
Exemplo n.º 4
0
    def setAuthByEmail(self, email: str):
        if stringEx.isNullOrWhitespace(email):
            return

        user = users.getUserByEmail(email)
        if user is not None:
            self.user_ = user
            se = session.getSessionByUser(user.user_id)
            if se is not None:
                self.session_ = se
Exemplo n.º 5
0
    def setAuthBySessionId(self, session_id: str):
        if stringEx.isNullOrWhitespace(session_id):
            return

        se = session.getSessionById(session_id)
        if se is not None:
            self.session_ = se
            user = users.getUserById(se.user_id)
            if user is not None:
                self.user_ = user
Exemplo n.º 6
0
def validateLogin(email, pwd):
    error = r'Invalid email or password.'
    if stringEx.isNullOrWhitespace(email) or stringEx.isNullOrWhitespace(pwd):
        return error

    try:
        sql = r'select count(*) from Users where Email = @email and Pwd =@pwd'
        param = SqlParameter()
        param.add('email', email)
        param.add('pwd', pwd)
        cmd = SqlCommand(sql, param)
        scalar = cmd.executeScalar()
    except:
        return "Invalid email data."
    finally:
        cmd.close()
    if scalar > 0:
        return ''
    else:
        return error
Exemplo n.º 7
0
def validateNickName(nickName):
    if stringEx.isNullOrWhitespace(nickName):
        return r"Nick name can't be empty."
    try:
        sql = r'select count(*) from Users where NickName = @nickName'
        param = SqlParameter()
        param.add('nickName', nickName)
        with SqlCommand(sql, param) as cmd:
            scalar = cmd.executeScalar()
            if scalar > 0:
                return r'The nick name has already existed.'
    except:
        return r'Invalid nick name.'
    return ''
Exemplo n.º 8
0
def validateEmail(email):
    if stringEx.isNullOrWhitespace(email):
        return r"email can't be empty."

    try:
        sql = r'select count(*) from Users where email = @email'
        param = SqlParameter()
        param.add('email', email)
        cmd = SqlCommand(sql, param)
        scalar = cmd.executeScalar()
    except:
        return "Invalid email data."
    finally:
        cmd.close()
    if scalar > 0:
        return r'The email has already existed.'
    else:
        return ''
Exemplo n.º 9
0
def login(request):
    if request.method == 'POST':
        email = request.POST['email']
        pwd = request.POST['pwd']
        error = users.validateLogin(email, pwd)
        if stringEx.isNullOrWhitespace(error):
            user = users.getUserByEmail(email)
            se = session.Session(user.user_id)
            session.addSession(se)
            auth = Auth()
            auth.setAuthByEmail(email)
            return render(request, 'DeanSpace/home.html', {'auth': auth})
        else:
            return render(request, 'DeanSpace/login.html', {
                'hasError': True,
                'errorMessage': error
            })
    elif request.method == 'GET':
        return render(request, 'DeanSpace/login.html')
Exemplo n.º 10
0
def getUserById(id):
    if stringEx.isNullOrWhitespace(id):
        return None

    cmd = None
    userInfo = None
    try:
        param = SqlParameter()
        param.add("id", id)
        sql = r'select Id, NickName, Email from Users where Id = @id'
        cmd = SqlCommand(sql, param)
        cursor = cmd.executeQuery()
        for row in cursor:
            userInfo = UserInfo()
            userInfo.user_id = id
            userInfo.nickName = row[1]
            userInfo.email = row[2]
            return userInfo
    except:
        return None
    finally:
        if cmd != None:
            cmd.close()