diff --git a/flag.go b/flag.go index 14341d8..b1b44b0 100644 --- a/flag.go +++ b/flag.go @@ -88,7 +88,7 @@ func createAdminCommand(name string) { password := string(passwordBytes) log.SetOutput(io.Discard) - err = user.Register(name, password, "admin", true) + err = user.Register(name, password, "admin", "local", true) log.SetOutput(wr) if err != nil { diff --git a/user/files.go b/user/files.go index 754f804..8b97bb0 100644 --- a/user/files.go +++ b/user/files.go @@ -40,6 +40,9 @@ func usersFromFile() []*User { } for _, u := range users { u.Name = util.CanonicalName(u.Name) + if u.Source == "" { + u.Source = "local" + } } log.Println("Found", len(users), "users") return users diff --git a/user/net.go b/user/net.go index 1d6d510..fdbfcce 100644 --- a/user/net.go +++ b/user/net.go @@ -40,7 +40,7 @@ func LogoutFromRequest(w http.ResponseWriter, rq *http.Request) { } // Register registers the given user. If it fails, a non-nil error is returned. -func Register(username, password, group string, force bool) error { +func Register(username, password, group, source string, force bool) error { username = util.CanonicalName(username) switch { @@ -48,6 +48,8 @@ func Register(username, password, group string, force bool) error { return fmt.Errorf("illegal username \"%s\"", username) case !ValidGroup(group): return fmt.Errorf("invalid group \"%s\"", group) + case !ValidSource(source): + return fmt.Errorf("invalid source \"%s\"", source) case HasUsername(username): return fmt.Errorf("username \"%s\" is already taken", username) case !force && cfg.RegistrationLimit > 0 && Count() >= cfg.RegistrationLimit: @@ -62,6 +64,7 @@ func Register(username, password, group string, force bool) error { u := User{ Name: username, Group: group, + Source: source, Password: string(hash), RegisteredAt: time.Now(), } diff --git a/user/user.go b/user/user.go index 81d04a3..a2e554d 100644 --- a/user/user.go +++ b/user/user.go @@ -16,6 +16,8 @@ type User struct { Group string `json:"group"` Password string `json:"hashed_password"` RegisteredAt time.Time `json:"registered_on"` + // Source is where the user from. Valid values: valid, telegram. + Source string `json:"source"` sync.RWMutex // A note about why HashedPassword is string and not []byte. The reason is @@ -44,17 +46,15 @@ var groups = []string{ "anon", "editor", "trusted", - "telegram", "moderator", "admin", } -// Group — Right +// Group — Right level var groupRight = map[string]int{ "anon": 0, "editor": 1, "trusted": 2, - "telegram": 2, "moderator": 3, "admin": 4, } @@ -68,11 +68,16 @@ func ValidGroup(group string) bool { return false } +func ValidSource(source string) bool { + return source == "local" || source == "telegram" +} + func EmptyUser() *User { return &User{ Name: "anon", Group: "anon", Password: "", + Source: "local", } } diff --git a/views/admin.qtpl b/views/admin.qtpl index 25d63cb..698114c 100644 --- a/views/admin.qtpl +++ b/views/admin.qtpl @@ -142,9 +142,6 @@ {% endif %} - {% if f.Get("group") == "telegram" %} -

This user is authorized using Telegram, they have the same rights as trusted editors. You cannot change their group.

- {% else %}
anon + editor + trusted + anon + qw422016.N().S(`>moderator editor - trusted - moderator - admin + qw422016.N().S(`>admin
@@ -448,122 +437,117 @@ func StreamAdminUserEditHTML(qw422016 *qt422016.Writer, u *user.User, f util.For
- `) -//line views/admin.qtpl:163 - } -//line views/admin.qtpl:163 - qw422016.N().S(`

Delete user

Remove the user from the database. Changes made by the user will be preserved. It will be possible to take this username later.

Delete `) -//line views/admin.qtpl:171 +//line views/admin.qtpl:167 } -//line views/admin.qtpl:171 +//line views/admin.qtpl:167 func WriteAdminUserEditHTML(qq422016 qtio422016.Writer, u *user.User, f util.FormData) { -//line views/admin.qtpl:171 +//line views/admin.qtpl:167 qw422016 := qt422016.AcquireWriter(qq422016) -//line views/admin.qtpl:171 +//line views/admin.qtpl:167 StreamAdminUserEditHTML(qw422016, u, f) -//line views/admin.qtpl:171 +//line views/admin.qtpl:167 qt422016.ReleaseWriter(qw422016) -//line views/admin.qtpl:171 +//line views/admin.qtpl:167 } -//line views/admin.qtpl:171 +//line views/admin.qtpl:167 func AdminUserEditHTML(u *user.User, f util.FormData) string { -//line views/admin.qtpl:171 +//line views/admin.qtpl:167 qb422016 := qt422016.AcquireByteBuffer() -//line views/admin.qtpl:171 +//line views/admin.qtpl:167 WriteAdminUserEditHTML(qb422016, u, f) -//line views/admin.qtpl:171 +//line views/admin.qtpl:167 qs422016 := string(qb422016.B) -//line views/admin.qtpl:171 +//line views/admin.qtpl:167 qt422016.ReleaseByteBuffer(qb422016) -//line views/admin.qtpl:171 +//line views/admin.qtpl:167 return qs422016 -//line views/admin.qtpl:171 +//line views/admin.qtpl:167 } -//line views/admin.qtpl:173 +//line views/admin.qtpl:169 func StreamAdminUserDeleteHTML(qw422016 *qt422016.Writer, u *user.User, f util.FormData) { -//line views/admin.qtpl:173 +//line views/admin.qtpl:169 qw422016.N().S(`

Delete user

`) -//line views/admin.qtpl:178 +//line views/admin.qtpl:174 if f.HasError() { -//line views/admin.qtpl:178 +//line views/admin.qtpl:174 qw422016.N().S(`
Error: `) -//line views/admin.qtpl:181 +//line views/admin.qtpl:177 qw422016.E().S(f.Error()) -//line views/admin.qtpl:181 +//line views/admin.qtpl:177 qw422016.N().S(`
`) -//line views/admin.qtpl:183 +//line views/admin.qtpl:179 } -//line views/admin.qtpl:183 +//line views/admin.qtpl:179 qw422016.N().S(`

Are you sure you want to delete `) -//line views/admin.qtpl:185 +//line views/admin.qtpl:181 qw422016.E().S(u.Name) -//line views/admin.qtpl:185 +//line views/admin.qtpl:181 qw422016.N().S(` from the database? This action is irreversible.

Cancel
`) -//line views/admin.qtpl:194 +//line views/admin.qtpl:190 } -//line views/admin.qtpl:194 +//line views/admin.qtpl:190 func WriteAdminUserDeleteHTML(qq422016 qtio422016.Writer, u *user.User, f util.FormData) { -//line views/admin.qtpl:194 +//line views/admin.qtpl:190 qw422016 := qt422016.AcquireWriter(qq422016) -//line views/admin.qtpl:194 +//line views/admin.qtpl:190 StreamAdminUserDeleteHTML(qw422016, u, f) -//line views/admin.qtpl:194 +//line views/admin.qtpl:190 qt422016.ReleaseWriter(qw422016) -//line views/admin.qtpl:194 +//line views/admin.qtpl:190 } -//line views/admin.qtpl:194 +//line views/admin.qtpl:190 func AdminUserDeleteHTML(u *user.User, f util.FormData) string { -//line views/admin.qtpl:194 +//line views/admin.qtpl:190 qb422016 := qt422016.AcquireByteBuffer() -//line views/admin.qtpl:194 +//line views/admin.qtpl:190 WriteAdminUserDeleteHTML(qb422016, u, f) -//line views/admin.qtpl:194 +//line views/admin.qtpl:190 qs422016 := string(qb422016.B) -//line views/admin.qtpl:194 +//line views/admin.qtpl:190 qt422016.ReleaseByteBuffer(qb422016) -//line views/admin.qtpl:194 +//line views/admin.qtpl:190 return qs422016 -//line views/admin.qtpl:194 +//line views/admin.qtpl:190 } diff --git a/web/admin.go b/web/admin.go index b22cf07..f118cae 100644 --- a/web/admin.go +++ b/web/admin.go @@ -192,7 +192,7 @@ func handlerAdminUserNew(w http.ResponseWriter, rq *http.Request) { // Create a user f := util.FormDataFromRequest(rq, []string{"name", "password", "group"}) - err := user.Register(f.Get("name"), f.Get("password"), f.Get("group"), true) + err := user.Register(f.Get("name"), f.Get("password"), f.Get("group"), "local", true) if err != nil { html := views.AdminUserNewHTML(f.WithError(err)) diff --git a/web/auth.go b/web/auth.go index f8e0979..eef0f7f 100644 --- a/web/auth.go +++ b/web/auth.go @@ -58,7 +58,7 @@ func handlerRegister(w http.ResponseWriter, rq *http.Request) { var ( username = rq.PostFormValue("username") password = rq.PostFormValue("password") - err = user.Register(username, password, "editor", false) + err = user.Register(username, password, "editor", "local", false) ) if err != nil { log.Printf("Failed to register \"%s\": %s", username, err.Error()) @@ -134,11 +134,12 @@ func handlerTelegramLogin(w http.ResponseWriter, rq *http.Request) { err = user.Register( username, "", // Password matters not + "editor", "telegram", false, ) ) - if user.HasUsername(username) && user.UserByName(username).Group == "telegram" { + if user.HasUsername(username) && user.UserByName(username).Source == "telegram" { // Problems is something we put blankets on. err = nil }