internal/status/wisski_user: Add String()
This commit is contained in:
parent
e4a46658ae
commit
3321b5d0ba
4 changed files with 58 additions and 13 deletions
|
|
@ -115,12 +115,12 @@ func (du drupalUser) checkCommonPassword(context wisski_distillery.Context, inst
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return status.RunErrorGroup(context.Stderr, status.Group[wstatus.User, error]{
|
return status.RunErrorGroup(context.Stderr, status.Group[wstatus.DrupalUser, error]{
|
||||||
PrefixString: func(item wstatus.User, index int) string {
|
PrefixString: func(item wstatus.DrupalUser, index int) string {
|
||||||
return fmt.Sprintf("User[%q]: ", item.Name)
|
return fmt.Sprintf("User[%q]: ", item.Name)
|
||||||
},
|
},
|
||||||
PrefixAlign: true,
|
PrefixAlign: true,
|
||||||
Handler: func(user wstatus.User, index int, writer io.Writer) error {
|
Handler: func(user wstatus.DrupalUser, index int, writer io.Writer) error {
|
||||||
pv, err := users.GetPasswordValidator(context.Context, string(user.Name))
|
pv, err := users.GetPasswordValidator(context.Context, string(user.Name))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ type WissKI struct {
|
||||||
NoPrefixes bool // TODO: Move this into the database
|
NoPrefixes bool // TODO: Move this into the database
|
||||||
Prefixes []string // list of prefixes
|
Prefixes []string // list of prefixes
|
||||||
Pathbuilders map[string]string // all the pathbuilders
|
Pathbuilders map[string]string // all the pathbuilders
|
||||||
Users []User // all the known users
|
Users []DrupalUser // all the known users
|
||||||
Grants []models.Grant
|
Grants []models.Grant
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,16 @@ package status
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
|
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
|
||||||
"golang.org/x/exp/slices"
|
"golang.org/x/exp/slices"
|
||||||
)
|
)
|
||||||
|
|
||||||
// User represents a WissKI User
|
// DrupalUser represents a WissKI DrupalUser
|
||||||
type User struct {
|
type DrupalUser struct {
|
||||||
UID phpx.Integer `json:"uid,omitempty"`
|
UID phpx.Integer `json:"uid,omitempty"`
|
||||||
Name phpx.String `json:"name,omitempty"`
|
Name phpx.String `json:"name,omitempty"`
|
||||||
Mail phpx.String `json:"mail,omitempty"`
|
Mail phpx.String `json:"mail,omitempty"`
|
||||||
|
|
@ -21,6 +23,41 @@ type User struct {
|
||||||
Roles UserRoles `json:"roles,omitempty"`
|
Roles UserRoles `json:"roles,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (du DrupalUser) String() string {
|
||||||
|
var builder strings.Builder
|
||||||
|
|
||||||
|
builder.WriteString("DrupalUser{")
|
||||||
|
defer builder.WriteString("}")
|
||||||
|
|
||||||
|
fmt.Fprintf(&builder, "UID: %d, ", du.UID)
|
||||||
|
fmt.Fprintf(&builder, "Name: %q, ", du.Name)
|
||||||
|
|
||||||
|
if du.Mail != "" {
|
||||||
|
fmt.Fprintf(&builder, "Mail: %q, ", du.Mail)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintf(&builder, "Status: %t, ", du.Status)
|
||||||
|
|
||||||
|
for _, tn := range []struct {
|
||||||
|
Name string
|
||||||
|
Time time.Time
|
||||||
|
}{
|
||||||
|
{"Created", du.Created.Time()},
|
||||||
|
{"Changed", du.Changed.Time()},
|
||||||
|
{"Access", du.Access.Time()},
|
||||||
|
{"Login", du.Login.Time()},
|
||||||
|
} {
|
||||||
|
if tn.Time.IsZero() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
fmt.Fprintf(&builder, "%s: %q, ", tn.Name, tn.Time.Format(time.Stamp))
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintf(&builder, "Roles: %s", du.Roles)
|
||||||
|
|
||||||
|
return builder.String()
|
||||||
|
}
|
||||||
|
|
||||||
// UserRole represents the role of a user
|
// UserRole represents the role of a user
|
||||||
type UserRole string
|
type UserRole string
|
||||||
|
|
||||||
|
|
@ -32,13 +69,12 @@ const (
|
||||||
// UserRoles represents a set of user roles for a given user
|
// UserRoles represents a set of user roles for a given user
|
||||||
type UserRoles map[UserRole]struct{}
|
type UserRoles map[UserRole]struct{}
|
||||||
|
|
||||||
// Has checks if the UserRole has the given role
|
func (ur UserRoles) String() string {
|
||||||
func (ur UserRoles) Has(role UserRole) (ok bool) {
|
return "[" + ur.string() + "]"
|
||||||
_, ok = ur[role]
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ur UserRoles) MarshalJSON() ([]byte, error) {
|
// String turns this UserRoles into a string
|
||||||
|
func (ur UserRoles) string() string {
|
||||||
roles := make([]string, len(ur))
|
roles := make([]string, len(ur))
|
||||||
i := 0
|
i := 0
|
||||||
for r := range ur {
|
for r := range ur {
|
||||||
|
|
@ -46,8 +82,17 @@ func (ur UserRoles) MarshalJSON() ([]byte, error) {
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
slices.Sort(roles) // for consistent marshaling
|
slices.Sort(roles) // for consistent marshaling
|
||||||
|
return strings.Join(roles, ", ")
|
||||||
|
}
|
||||||
|
|
||||||
return json.Marshal(strings.Join(roles, ", "))
|
func (ur UserRoles) MarshalJSON() ([]byte, error) {
|
||||||
|
return json.Marshal(ur.string())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Has checks if the UserRole has the given role
|
||||||
|
func (ur UserRoles) Has(role UserRole) (ok bool) {
|
||||||
|
_, ok = ur[role]
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserRoles) UnmarshalJSON(data []byte) error {
|
func (u *UserRoles) UnmarshalJSON(data []byte) error {
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ var (
|
||||||
var usersPHP string
|
var usersPHP string
|
||||||
|
|
||||||
// All returns all known usernames
|
// All returns all known usernames
|
||||||
func (u *Users) All(ctx context.Context, server *phpx.Server) (users []status.User, err error) {
|
func (u *Users) All(ctx context.Context, server *phpx.Server) (users []status.DrupalUser, err error) {
|
||||||
err = u.Dependencies.PHP.ExecScript(ctx, server, &users, usersPHP, "list_users")
|
err = u.Dependencies.PHP.ExecScript(ctx, server, &users, usersPHP, "list_users")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue