From 12bf50bb961499d3154464686ac73b8e6dce2ef9 Mon Sep 17 00:00:00 2001 From: Umputun Date: Wed, 6 Jun 2018 14:26:04 -0500 Subject: [PATCH] add admin email #73 --- app/main.go | 24 +++++++++++++++++------- app/main_test.go | 5 ++++- app/rest/api/rest_public.go | 2 ++ app/rest/api/rest_public_test.go | 1 + app/rest/api/rest_test.go | 1 + app/rest/auth/auth.go | 1 + 6 files changed, 26 insertions(+), 8 deletions(-) diff --git a/app/main.go b/app/main.go index 556a7128..3aada293 100644 --- a/app/main.go +++ b/app/main.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "net/http" + "net/url" "os" "os/signal" "strings" @@ -27,13 +28,13 @@ import ( // Opts with command line flags and env type Opts struct { - BoltPath string `long:"bolt" env:"BOLTDB_PATH" default:"./var" description:"parent dir for bolt files"` - Sites []string `long:"site" env:"SITE" default:"remark" description:"site names" env-delim:","` - RemarkURL string `long:"url" env:"REMARK_URL" default:"https://remark42.com" description:"url to remark"` - Admins []string `long:"admin" env:"ADMIN" description:"admin(s) names" env-delim:","` - - DevPasswd string `long:"dev-passwd" env:"DEV_PASSWD" default:"" description:"development mode password"` - Dbg bool `long:"dbg" env:"DEBUG" description:"debug mode"` + BoltPath string `long:"bolt" env:"BOLTDB_PATH" default:"./var" description:"parent dir for bolt files"` + Sites []string `long:"site" env:"SITE" default:"remark" description:"site names" env-delim:","` + RemarkURL string `long:"url" env:"REMARK_URL" default:"https://remark42.com" description:"url to remark"` + Admins []string `long:"admin" env:"ADMIN" description:"admin(s) names" env-delim:","` + AdminEmail string `long:"admin-email" env:"ADMIN_EMAIL" default:"" description:"admin email"` + DevPasswd string `long:"dev-passwd" env:"DEV_PASSWD" default:"" description:"development mode password"` + Dbg bool `long:"dbg" env:"DEBUG" description:"debug mode"` BackupLocation string `long:"backup" env:"BACKUP_PATH" default:"./var/backup" description:"backups location"` MaxBackupFiles int `long:"max-back" env:"MAX_BACKUP_FILES" default:"10" description:"max backups to keep"` @@ -154,11 +155,20 @@ func New(opts Opts) (*Application, error) { Authenticator: auth.Authenticator{ JWTService: jwtService, Admins: opts.Admins, + AdminEmail: opts.AdminEmail, Providers: makeAuthProviders(jwtService, avatarProxy, dataService, opts), DevPasswd: opts.DevPasswd, }, Cache: loadingCache, } + + // no admin email, use admin@domain + if srv.Authenticator.AdminEmail == "" { + if u, err := url.Parse(opts.RemarkURL); err == nil { + srv.Authenticator.AdminEmail = "admin@" + u.Host + } + } + srv.ScoreThresholds.Low, srv.ScoreThresholds.Critical = opts.LowScore, opts.CriticalScore tch := make(chan struct{}) return &Application{restSrv: srv, migratorSrv: migr, exporter: exporter, Opts: opts, terminated: tch}, nil diff --git a/app/main_test.go b/app/main_test.go index be4cd760..2999260b 100644 --- a/app/main_test.go +++ b/app/main_test.go @@ -38,6 +38,9 @@ func TestApplication(t *testing.T) { assert.Equal(t, http.StatusCreated, resp.StatusCode) body, _ = ioutil.ReadAll(resp.Body) t.Log(string(body)) + + assert.Equal(t, "admin@demo.remark42.com", app.restSrv.Authenticator.AdminEmail, "default admin email") + app.Wait() } @@ -64,7 +67,7 @@ func prepApp(t *testing.T, port int, duration time.Duration) (*Application, cont // prepare options opts := Opts{} p := flags.NewParser(&opts, flags.Default) - p.ParseArgs([]string{"--secret=123456", "--dev-passwd=password"}) + p.ParseArgs([]string{"--secret=123456", "--dev-passwd=password", "--url=https://demo.remark42.com"}) opts.AvatarStore, opts.BackupLocation = "/tmp", "/tmp" opts.BoltPath = fmt.Sprintf("/tmp/%d", port) opts.GithubCSEC, opts.GithubCID = "csec", "cid" diff --git a/app/rest/api/rest_public.go b/app/rest/api/rest_public.go index e4e9a8f6..1eecdcef 100644 --- a/app/rest/api/rest_public.go +++ b/app/rest/api/rest_public.go @@ -204,6 +204,7 @@ func (s *Rest) configCtrl(w http.ResponseWriter, r *http.Request) { EditDuration int `json:"edit_duration"` MaxCommentSize int `json:"max_comment_size"` Admins []string `json:"admins"` + AdminEmail string `json:"admin_email"` Auth []string `json:"auth_providers"` LowScore int `json:"low_score"` CriticalScore int `json:"critical_score"` @@ -215,6 +216,7 @@ func (s *Rest) configCtrl(w http.ResponseWriter, r *http.Request) { EditDuration: int(s.DataService.EditDuration.Seconds()), MaxCommentSize: s.DataService.MaxCommentSize, Admins: s.Authenticator.Admins, + AdminEmail: s.Authenticator.AdminEmail, LowScore: s.ScoreThresholds.Low, CriticalScore: s.ScoreThresholds.Critical, ReadOnlyAge: s.ReadOnlyAge, diff --git a/app/rest/api/rest_public_test.go b/app/rest/api/rest_public_test.go index d6663dfc..27654b31 100644 --- a/app/rest/api/rest_public_test.go +++ b/app/rest/api/rest_public_test.go @@ -381,6 +381,7 @@ func TestRest_Config(t *testing.T) { assert.Nil(t, err) assert.Equal(t, 300., j["edit_duration"]) assert.EqualValues(t, []interface{}([]interface{}{"a1", "a2"}), j["admins"]) + assert.Equal(t, "admin@remark-42.com", j["admin_email"]) assert.Equal(t, 4000., j["max_comment_size"]) assert.Equal(t, -5., j["low_score"]) assert.Equal(t, -10., j["critical_score"]) diff --git a/app/rest/api/rest_test.go b/app/rest/api/rest_test.go index 5866ff88..934edcc1 100644 --- a/app/rest/api/rest_test.go +++ b/app/rest/api/rest_test.go @@ -58,6 +58,7 @@ func prep(t *testing.T) (srv *Rest, ts *httptest.Server) { DevPasswd: "password", Providers: nil, Admins: []string{"a1", "a2"}, + AdminEmail: "admin@remark-42.com", JWTService: auth.NewJWT("12345", false, time.Minute), }, Exporter: &migrator.Remark{DataStore: &dataStore}, diff --git a/app/rest/auth/auth.go b/app/rest/auth/auth.go index 4cf17464..fb75a3e4 100644 --- a/app/rest/auth/auth.go +++ b/app/rest/auth/auth.go @@ -16,6 +16,7 @@ type Authenticator struct { JWTService *JWT Providers []Provider Admins []string + AdminEmail string DevPasswd string }