forked from phc/website
Estratto il servizio lista utenti e qualche altra modifica
parent
85a0a21d15
commit
805d9a7ba5
@ -0,0 +1,43 @@
|
|||||||
|
package lista_utenti
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.phc.dm.unipi.it/phc/website/auth"
|
||||||
|
"git.phc.dm.unipi.it/phc/website/model"
|
||||||
|
"git.phc.dm.unipi.it/phc/website/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
type authListaUtenti struct {
|
||||||
|
AuthService auth.Service
|
||||||
|
|
||||||
|
Macchinisti util.Set[string]
|
||||||
|
}
|
||||||
|
|
||||||
|
func newAuthListaUtenti(authService auth.Service) (Service, error) {
|
||||||
|
macchinisti, err := loadMacchinisti()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &authListaUtenti{authService, macchinisti}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *authListaUtenti) GetUtenti() ([]*model.ListUser, error) {
|
||||||
|
authUsers, err := a.AuthService.GetUsers()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
users := make([]*model.ListUser, 0, len(authUsers))
|
||||||
|
for _, u := range authUsers {
|
||||||
|
users = append(users, &model.ListUser{
|
||||||
|
Uid: u.Username,
|
||||||
|
Nome: u.Name,
|
||||||
|
Cognome: u.Surname,
|
||||||
|
Tags: []string{},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
mergeMacchinisti(users, a.Macchinisti)
|
||||||
|
|
||||||
|
return users, nil
|
||||||
|
}
|
||||||
@ -0,0 +1,44 @@
|
|||||||
|
package lista_utenti
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"git.phc.dm.unipi.it/phc/website/model"
|
||||||
|
"git.phc.dm.unipi.it/phc/website/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
type jsonListaUtenti struct {
|
||||||
|
Path string
|
||||||
|
|
||||||
|
Macchinisti util.Set[string]
|
||||||
|
}
|
||||||
|
|
||||||
|
func newJsonListaUtenti(path string) (Service, error) {
|
||||||
|
macchinisti, err := loadMacchinisti()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &jsonListaUtenti{path, macchinisti}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j *jsonListaUtenti) GetUtenti() ([]*model.ListUser, error) {
|
||||||
|
var users []*model.ListUser
|
||||||
|
|
||||||
|
f, err := os.Open(j.Path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.NewDecoder(f).Decode(&users); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
mergeMacchinisti(users, j.Macchinisti)
|
||||||
|
|
||||||
|
log.Printf("Caricata lista di %d utenti", len(users))
|
||||||
|
|
||||||
|
return users, nil
|
||||||
|
}
|
||||||
@ -0,0 +1,46 @@
|
|||||||
|
package lista_utenti
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"git.phc.dm.unipi.it/phc/website/auth"
|
||||||
|
"git.phc.dm.unipi.it/phc/website/model"
|
||||||
|
"git.phc.dm.unipi.it/phc/website/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Service interface {
|
||||||
|
GetUtenti() ([]*model.ListUser, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// New crea un nuovo servizio che lista gli utenti utilizzando AuthService se config è ":auth:" oppure un file json
|
||||||
|
func New(authService auth.Service, config string) (Service, error) {
|
||||||
|
if config == ":auth:" {
|
||||||
|
return newAuthListaUtenti(authService)
|
||||||
|
}
|
||||||
|
|
||||||
|
return newJsonListaUtenti(config)
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadMacchinisti() (util.Set[string], error) {
|
||||||
|
f, err := os.Open("macchinisti.json")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
macchinisti := []string{}
|
||||||
|
|
||||||
|
if err := json.NewDecoder(f).Decode(&macchinisti); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return util.NewSet(macchinisti...), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func mergeMacchinisti(users []*model.ListUser, macchinisti util.Set[string]) {
|
||||||
|
for _, user := range users {
|
||||||
|
if _, found := macchinisti[user.Uid]; found {
|
||||||
|
user.Tags = append(user.Tags, "macchinista")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1 @@
|
|||||||
|
["delucreziis", "serdyuk", "minnocci", "manicastri"]
|
||||||
@ -0,0 +1,26 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
type Set[T comparable] map[T]struct{}
|
||||||
|
|
||||||
|
func NewSet[T comparable](elements ...T) Set[T] {
|
||||||
|
set := Set[T]{}
|
||||||
|
|
||||||
|
for _, e := range elements {
|
||||||
|
set[e] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
return set
|
||||||
|
}
|
||||||
|
|
||||||
|
func (set Set[T]) Add(value T) {
|
||||||
|
set[value] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (set Set[T]) Remove(value T) {
|
||||||
|
delete(set, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (set Set[T]) Has(value T) bool {
|
||||||
|
_, found := set[value]
|
||||||
|
return found
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue