3
0

Add FindByAny, Stringer for users

This commit is contained in:
Denis Arh 2019-12-24 10:07:45 +01:00
parent 95b49e7061
commit e7504dd35f
4 changed files with 31 additions and 0 deletions

View File

@ -1,5 +1,9 @@
package auth
import (
"fmt"
)
type (
Identity struct {
id uint64
@ -30,6 +34,10 @@ func (i Identity) Valid() bool {
return i.id > 0
}
func (i Identity) String() string {
return fmt.Sprintf("%d", i.id)
}
func NewSuperUserIdentity() *Identity {
return NewIdentity(superUserID)
}

View File

@ -9,6 +9,7 @@ type (
Identity() uint64
Roles() []uint64
Valid() bool
String() string
}
TokenEncoder interface {

View File

@ -3,6 +3,8 @@ package service
import (
"context"
"io"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/titpetric/factory"
@ -79,6 +81,7 @@ type (
FindByEmail(email string) (*types.User, error)
FindByHandle(handle string) (*types.User, error)
FindByID(id uint64) (*types.User, error)
FindByAny(any string) (*types.User, error)
Find(types.UserFilter) (types.UserSet, types.UserFilter, error)
Create(input *types.User) (*types.User, error)
@ -154,6 +157,20 @@ func (svc user) FindByHandle(handle string) (*types.User, error) {
return svc.proc(svc.user.FindByHandle(handle))
}
func (svc user) FindByAny(any string) (*types.User, error) {
return svc.proc(func() (*types.User, error) {
if id, _ := strconv.ParseUint(any, 10, 64); id > 0 {
return svc.user.FindByID(id)
}
if strings.Contains(any, "@") {
return svc.user.FindByEmail(any)
}
return svc.user.FindByHandle(any)
}())
}
func (svc user) proc(u *types.User, err error) (*types.User, error) {
if err != nil {
return nil, err

View File

@ -3,6 +3,7 @@ package types
import (
"database/sql/driver"
"encoding/json"
"fmt"
"time"
"github.com/pkg/errors"
@ -89,6 +90,10 @@ const (
BotUser UserKind = "bot"
)
func (u User) String() string {
return fmt.Sprintf("%d", u.ID)
}
func (u *User) Valid() bool {
return u.ID > 0 && u.SuspendedAt == nil && u.DeletedAt == nil
}