mirror of
https://github.com/micromdm/micromdm/
synced 2026-08-07 18:15:47 +08:00
Add FindUser method.
Returns the user by id.
This commit is contained in:
@@ -62,6 +62,26 @@ func (d *Postgres) ConfirmUser(ctx context.Context, confirmation string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Postgres) FindUser(ctx context.Context, id string) (*User, error) {
|
||||
|
||||
u := &User{}
|
||||
q := fmt.Sprintf(`SELECT %s FROM users WHERE id = $1;`, strings.Join(columns(), `, `))
|
||||
if err := d.db.QueryRow(ctx, q, id).Scan(
|
||||
&u.ID,
|
||||
&u.Username,
|
||||
&u.Email,
|
||||
&u.Password,
|
||||
&u.Salt,
|
||||
&u.ConfirmationHash,
|
||||
&u.CreatedAt,
|
||||
&u.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return u, nil
|
||||
}
|
||||
|
||||
func (d *Postgres) FindUserByEmail(ctx context.Context, email string) (*User, error) {
|
||||
if email == "" {
|
||||
return nil, Error{invalid: constraints["chk_email_not_empty"]}
|
||||
|
||||
@@ -88,6 +88,29 @@ func (d *SQLite) ConfirmUser(ctx context.Context, confirmation string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *SQLite) FindUser(ctx context.Context, id string) (*User, error) {
|
||||
conn := d.db.Get(ctx)
|
||||
if conn == nil {
|
||||
return nil, context.Canceled
|
||||
}
|
||||
defer d.db.Put(conn)
|
||||
|
||||
stmt := conn.Prep(fmt.Sprintf(
|
||||
`SELECT %s FROM users WHERE id = $id;`, strings.Join(columns(), `, `)))
|
||||
stmt.SetText("$id", id)
|
||||
if found, err := stmt.Step(); err != nil {
|
||||
return nil, err
|
||||
} else if !found {
|
||||
return nil, fmt.Errorf("did not find user with id %q", id)
|
||||
}
|
||||
|
||||
usr, err := sqliteUser(stmt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return usr, stmt.Reset()
|
||||
}
|
||||
|
||||
func (d *SQLite) FindUserByEmail(ctx context.Context, email string) (*User, error) {
|
||||
conn := d.db.Get(ctx)
|
||||
if conn == nil {
|
||||
|
||||
Reference in New Issue
Block a user