Exemplo n.º 1
0
// Create creates a new session using a key generated for the given User
func (m *SessionManager) Create(user User) (session Session) {
	// Set the expires from the cookie config
	session = Session{
		Expires: m.nowFunc().Add(m.cookie.Age),
		UserID:  user.ID,
		manager: m,
	}

	// Generate a new random session key
	for {
		session.Key = m.keyFunc()

		// No duplicates - generate a new key if this key already exists
		var duplicate string
		stmt := sol.Select(
			Sessions.C("key"),
		).Where(Sessions.C("key").Equals(session.Key)).Limit(1)
		m.conn.Query(stmt, &duplicate)
		if duplicate == "" {
			break
		}
	}

	// Insert the session
	m.conn.Query(Sessions.Insert().Values(session))
	return
}
Exemplo n.º 2
0
func TestSelect(t *testing.T) {
	expect := sol.NewTester(t, &PostGres{})

	// Build a GROUP BY statement using an aggregate
	expect.SQL(
		sol.Select(
			things.C("name"),
			sol.Max(things.C("created_at")),
		).GroupBy(
			things.C("name"),
		).OrderBy(
			sol.Max(things.C("created_at")).Desc(),
		),
		`SELECT things.name, MAX(things.created_at) FROM things GROUP BY things.name ORDER BY MAX(things.created_at) DESC`,
	)
}
Exemplo n.º 3
0
Arquivo: users.go Projeto: aodin/volta
// createUser checks for a duplicate email before inserting the user.
// Email must already be normalized.
func (m *UserManager) createUser(user *User) error {
	email := sol.Select(
		Users.C("email"),
	).Where(Users.C("email").Equals(user.Email)).Limit(1)

	var duplicate string
	m.conn.Query(email, &duplicate)
	if duplicate != "" {
		return fmt.Errorf(
			"auth: user with email %s already exists", duplicate,
		)
	}

	// Insert the new user
	m.conn.Query(postgres.Insert(Users).Values(user).Returning(), user)
	return nil
}
Exemplo n.º 4
0
// Create creates a new token for the user. It will panic on error. The user
// ID must exist.
func (m *TokenManager) ForeverToken(user User) (token Token) {
	token.UserID = user.ID
	token.manager = m

	// Generate a new token
	for {
		token.Key = m.keyFunc()

		// No duplicates - generate a new key if this key already exists
		stmt := sol.Select(
			Tokens.C("key"),
		).Where(Tokens.C("key").Equals(token.Key)).Limit(1)
		var duplicate string
		m.conn.Query(stmt, &duplicate)
		if duplicate == "" {
			break
		}
	}

	// Insert the token into the database
	m.conn.Query(postgres.Insert(Tokens).Values(token).Returning(), &token)
	return
}
Exemplo n.º 5
0
func (m *TokenManager) Count() (count int64) {
	m.conn.Query(sol.Select(sol.Count(Tokens.C("key"))), &count)
	return
}