Auth: Improve legacy user search command and add tests #98
Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
parent
0be163370d
commit
6b8b3bf3c2
4 changed files with 70 additions and 12 deletions
|
@ -13,10 +13,11 @@ import (
|
|||
|
||||
// UsersLegacyCommand configures the command name, flags, and action.
|
||||
var UsersLegacyCommand = cli.Command{
|
||||
Name: "legacy",
|
||||
Usage: "Displays legacy user accounts",
|
||||
Flags: report.CliFlags,
|
||||
Action: usersLegacyAction,
|
||||
Name: "legacy",
|
||||
Usage: "Displays legacy user accounts",
|
||||
ArgsUsage: "[search]",
|
||||
Flags: report.CliFlags,
|
||||
Action: usersLegacyAction,
|
||||
}
|
||||
|
||||
// usersLegacyAction displays legacy user accounts.
|
||||
|
@ -25,7 +26,7 @@ func usersLegacyAction(ctx *cli.Context) error {
|
|||
cols := []string{"ID", "UID", "User Name", "Display Name", "Email", "Admin", "Created At"}
|
||||
|
||||
// Fetch users from database.
|
||||
users := entity.FindLegacyUsers()
|
||||
users := entity.FindLegacyUsers(ctx.Args().First())
|
||||
rows := make([][]string, len(users))
|
||||
|
||||
// Show log message.
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"github.com/photoprism/photoprism/internal/config"
|
||||
"github.com/photoprism/photoprism/internal/query"
|
||||
"github.com/photoprism/photoprism/pkg/report"
|
||||
"github.com/photoprism/photoprism/pkg/txt"
|
||||
)
|
||||
|
||||
// UsersListCommand configures the command name, flags, and action.
|
||||
|
@ -22,7 +23,7 @@ var UsersListCommand = cli.Command{
|
|||
// usersListAction displays existing user accounts.
|
||||
func usersListAction(ctx *cli.Context) error {
|
||||
return CallWithDependencies(ctx, func(conf *config.Config) error {
|
||||
cols := []string{"UID", "User Name", "Display Name", "Email", "Role", "Super Admin", "Web Login", "WebDAV", "Attributes"}
|
||||
cols := []string{"ID", "UID", "User Name", "Display Name", "Email", "Role", "Super Admin", "Web Login", "WebDAV", "Attributes", "Created At"}
|
||||
|
||||
// Fetch users from database.
|
||||
users := query.RegisteredUsers()
|
||||
|
@ -34,6 +35,7 @@ func usersListAction(ctx *cli.Context) error {
|
|||
// Display report.
|
||||
for i, user := range users {
|
||||
rows[i] = []string{
|
||||
fmt.Sprintf("%d", user.ID),
|
||||
user.UID(),
|
||||
user.Name(),
|
||||
user.FullName(),
|
||||
|
@ -43,6 +45,7 @@ func usersListAction(ctx *cli.Context) error {
|
|||
report.Bool(user.CanLogIn(), report.Enabled, report.Disabled),
|
||||
report.Bool(user.CanUseWebDAV(), report.Enabled, report.Disabled),
|
||||
user.Attr(),
|
||||
txt.TimeStamp(&user.CreatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,12 @@
|
|||
package entity
|
||||
|
||||
import "github.com/photoprism/photoprism/internal/entity/legacy"
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/photoprism/photoprism/internal/entity/legacy"
|
||||
"github.com/photoprism/photoprism/pkg/rnd"
|
||||
"github.com/photoprism/photoprism/pkg/txt"
|
||||
)
|
||||
|
||||
// FindLegacyUser returns the matching legacy user or nil if it was not found.
|
||||
func FindLegacyUser(find User) *legacy.User {
|
||||
|
@ -29,10 +35,26 @@ func FindLegacyUser(find User) *legacy.User {
|
|||
}
|
||||
|
||||
// FindLegacyUsers finds registered legacy users.
|
||||
func FindLegacyUsers() legacy.Users {
|
||||
result := make(legacy.Users, 0, 1)
|
||||
func FindLegacyUsers(search string) legacy.Users {
|
||||
result := legacy.Users{}
|
||||
|
||||
Db().Where("id > 0").Find(&result)
|
||||
stmt := Db()
|
||||
|
||||
search = strings.TrimSpace(search)
|
||||
|
||||
if search == "all" {
|
||||
// Don't filter.
|
||||
} else if id := txt.Int(search); id != 0 {
|
||||
stmt = stmt.Where("id = ?", id)
|
||||
} else if rnd.IsUID(search, UserUID) {
|
||||
stmt = stmt.Where("user_uid = ?", search)
|
||||
} else if search != "" {
|
||||
stmt = stmt.Where("user_name LIKE ? OR primary_email LIKE ? OR full_name LIKE ?", search+"%", search+"%", search+"%")
|
||||
} else {
|
||||
stmt = stmt.Where("id > 0")
|
||||
}
|
||||
|
||||
stmt.Order("id").Find(&result)
|
||||
|
||||
return result
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ func TestFindLegacyUser(t *testing.T) {
|
|||
notFound := FindLegacyUser(Admin)
|
||||
assert.Nil(t, notFound)
|
||||
|
||||
t.Logf("Legacy Admin: %#v", notFound)
|
||||
// t.Logf("Legacy Admin: %#v", notFound)
|
||||
|
||||
if err := Db().AutoMigrate(legacy.User{}).Error; err != nil {
|
||||
log.Debugf("TestFindLegacyUser: %s (waiting 1s)", err.Error())
|
||||
|
@ -31,7 +31,39 @@ func TestFindLegacyUser(t *testing.T) {
|
|||
found := FindLegacyUser(Admin)
|
||||
assert.NotNil(t, found)
|
||||
|
||||
t.Logf("Legacy Admin: %#v", found)
|
||||
// t.Logf("Legacy Admin: %#v", found)
|
||||
|
||||
if err := Db().DropTable(legacy.User{}).Error; err != nil {
|
||||
log.Errorf("TestFindLegacyUser: failed dropping legacy.User")
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindLegacyUsers(t *testing.T) {
|
||||
notFound := FindLegacyUsers("all")
|
||||
assert.Len(t, notFound, 0)
|
||||
|
||||
// t.Logf("Legacy Users: %#v", notFound)
|
||||
|
||||
if err := Db().AutoMigrate(legacy.User{}).Error; err != nil {
|
||||
log.Debugf("TestFindLegacyUser: %s (waiting 1s)", err.Error())
|
||||
|
||||
time.Sleep(time.Second)
|
||||
|
||||
if err = Db().AutoMigrate(legacy.User{}).Error; err != nil {
|
||||
log.Errorf("TestFindLegacyUser: failed migrating legacy.User")
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
Db().Save(legacy.Admin)
|
||||
|
||||
found := FindLegacyUsers("all")
|
||||
|
||||
assert.NotNil(t, found)
|
||||
assert.Len(t, found, 1)
|
||||
|
||||
// t.Logf("Legacy Users: %#v", found)
|
||||
|
||||
if err := Db().DropTable(legacy.User{}).Error; err != nil {
|
||||
log.Errorf("TestFindLegacyUser: failed dropping legacy.User")
|
||||
|
|
Loading…
Reference in a new issue