Ejemplo n.º 1
0
func LoadByUsername(username string) (*User, error) {
	u := new(User)

	q := `
        SELECT
            u.id
            , u.key
            , u.active
            , u.username
            , u.email
            , u.fullname
        FROM user u
        WHERE u.username = ?;
    `

	params := []interface{}{
		username,
	}

	bind := []interface{}{
		&u.Id,
		&u.Key,
		&u.Active,
		&u.Username,
		&u.Email,
		&u.Fullname,
	}

	err := dao.Row(q, params, bind)

	return u, err
}
Ejemplo n.º 2
0
func Parse(r *http.Request) (*Session, error) {
	c, err := r.Cookie(config.Get("session_cookie_name"))
	if err != nil {
		return nil, err
	}

	q := `
        SELECT
            s.id
            , u.key
        FROM user u, user_session s
        WHERE u.id = s.user_id
        AND u.active = 1
        AND s.key = ?
        AND s.valid_until > ?;
    `

	params := []interface{}{
		c.Value,
		time.Now().Unix(),
	}

	var sid int64
	var ukey string

	bind := []interface{}{
		&sid,
		&ukey,
	}

	err = dao.Row(q, params, bind)
	if err != nil {
		return nil, err
	}

	u, err := user.Load(ukey)
	if err != nil {
		return nil, err
	}

	return &Session{
		Id:   sid,
		Key:  c.Value,
		User: u,
	}, nil
}
Ejemplo n.º 3
0
func Login(username string, password string) (*User, error) {
	u := new(User)

	q := `
        SELECT
            u.id
            , u.key
            , u.active
            , u.username
            , u.email
            , u.fullname
        FROM user u
        WHERE u.active = 1
        AND (
            u.username = ?
            OR u.email = ?
        )
        AND u.password = ?;
    `

	params := []interface{}{
		username,
		username,
		hashpassword(password),
	}

	bind := []interface{}{
		&u.Id,
		&u.Key,
		&u.Active,
		&u.Username,
		&u.Email,
		&u.Fullname,
	}

	err := dao.Row(q, params, bind)

	return u, err
}
Ejemplo n.º 4
0
func Verify(vkey string) (*User, error) {
	// Get user key from verify
	q := `
        SELECT
            u.key
        FROM user_verify v, user u
        WHERE v.user_id = u.id
        AND v.key = ?
        AND v.valid_until > ?;
    `

	params := []interface{}{
		vkey,
		time.Now().Unix(),
	}

	var ukey string

	bind := []interface{}{
		&ukey,
	}

	err := dao.Row(q, params, bind)
	if err != nil {
		return nil, err
	}

	// Activate user
	q = `
        UPDATE user SET
            active = ?
            , modified_date = ?
        WHERE key = ?;
    `

	params = []interface{}{
		1,
		time.Now().Unix(),
		ukey,
	}

	_, err = dao.Exec(q, params)
	if err != nil {
		return nil, err
	}

	u, err := Load(ukey)
	if err != nil {
		return nil, err
	}

	q = `
        UPDATE user_verify SET
            valid_until = 0
            , modified_date = ?
        WHERE key = ?;
    `

	params = []interface{}{
		time.Now().Unix(),
		vkey,
	}

	_, err = dao.Exec(q, params)
	if err != nil {
		return nil, err
	}

	return u, nil
}