diff --git a/Makefile b/Makefile index c48dc4f..2bacbea 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,9 @@ run: build config_run: build ./mycorrhiza -config-path "assets/config.ini" metarrhiza +devconfig_run: build + ./mycorrhiza -config-path "assets/devconfig.ini" metarrhiza + build: go generate go build . diff --git a/assets/assets.qtpl.go b/assets/assets.qtpl.go index efbe143..ed269e1 100644 --- a/assets/assets.qtpl.go +++ b/assets/assets.qtpl.go @@ -339,7 +339,6 @@ table { background-color: #eee; } /* Other stuff */ html { background-color: #ddd; -background-image: url("data:image/svg+xml,%3Csvg width='42' height='44' viewBox='0 0 42 44' xmlns='http://www.w3.org/2000/svg'%3E%3Cg id='Page-1' fill='none' fill-rule='evenodd'%3E%3Cg id='brick-wall' fill='%23bbbbbb' fill-opacity='0.4'%3E%3Cpath d='M0 0h42v44H0V0zm1 1h40v20H1V1zM0 23h20v20H0V23zm22 0h20v20H22V23z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E"); background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100' height='199' viewBox='0 0 100 199'%3E%3Cg fill='%23bbbbbb' %3E%3Cpath d='M0 199V0h1v1.99L100 199h-1.12L1 4.22V199H0zM100 2h-.12l-1-2H100v2z'%3E%3C/path%3E%3C/g%3E%3C/svg%3E"); } /* heropatterns.com */ header { background-color: #bbb; } @@ -468,8 +467,8 @@ function selectionWrapper(cursorPosition, prefix, postfix = null, el = editTexta if (postfix == null) { postfix = prefix } - text = getSelectedText(el) - result = prefix + text + postfix + let text = getSelectedText(el) + let result = prefix + text + postfix el.setRangeText(result, start, end, 'select') el.focus() placeCursor(end + cursorPosition) diff --git a/assets/devconfig.ini b/assets/devconfig.ini new file mode 100644 index 0000000..4a26789 --- /dev/null +++ b/assets/devconfig.ini @@ -0,0 +1,19 @@ +WikiName = Mycorrhiza (dev) +NaviTitleIcon = 🧑‍💻 + +[Hyphae] +HomeHypha = home +UserHypha = u +HeaderLinksHypha = header-links + +[Network] +HTTPPort = 1737 +URL = http://localhost:1737 + +[Authorization] +UseFixedAuth = true +FixedAuthCredentialsPath = mycocredentials.json + +UseRegistration = true +RegistrationCredentialsPath = mycoregistration.json +LimitRegistration = 3 diff --git a/assets/toolbar.js b/assets/toolbar.js index 08ae31e..c631a96 100644 --- a/assets/toolbar.js +++ b/assets/toolbar.js @@ -30,8 +30,8 @@ function selectionWrapper(cursorPosition, prefix, postfix = null, el = editTexta if (postfix == null) { postfix = prefix } - text = getSelectedText(el) - result = prefix + text + postfix + let text = getSelectedText(el) + let result = prefix + text + postfix el.setRangeText(result, start, end, 'select') el.focus() placeCursor(end + cursorPosition) diff --git a/flag.go b/flag.go index 8dec37d..6c767b3 100644 --- a/flag.go +++ b/flag.go @@ -59,7 +59,4 @@ func parseCliArgs() { util.UserHypha = util.CanonicalName(util.UserHypha) util.HeaderLinksHypha = util.CanonicalName(util.HeaderLinksHypha) user.AuthUsed = util.UseFixedAuth || util.UseRegistration - if user.AuthUsed && (util.FixedCredentialsPath != "" || util.RegistrationCredentialsPath != "") { - user.ReadUsersFromFilesystem() - } } diff --git a/main.go b/main.go index 1ce8cf2..8b66ca0 100644 --- a/main.go +++ b/main.go @@ -186,6 +186,9 @@ func main() { hyphae.Index(WikiDir) log.Println("Indexed", hyphae.Count(), "hyphae") + if user.AuthUsed && (util.FixedCredentialsPath != "" || util.RegistrationCredentialsPath != "") { + user.ReadUsersFromFilesystem() + } history.Start(WikiDir) shroom.SetHeaderLinks() diff --git a/user/files.go b/user/files.go index 721b82c..79736f2 100644 --- a/user/files.go +++ b/user/files.go @@ -13,13 +13,20 @@ import ( // ReadUsersFromFilesystem reads all user information from filesystem and stores it internally. Call it during initialization. func ReadUsersFromFilesystem() { - rememberUsers(usersFromFixedCredentials()) + if util.UseFixedAuth { + rememberUsers(usersFromFixedCredentials()) + } + if util.UseRegistration { + rememberUsers(usersFromRegistrationCredentials()) + } readTokensToUsers() } -func usersFromFixedCredentials() []*User { - var users []*User - contents, err := ioutil.ReadFile(util.FixedCredentialsPath) +func usersFromFile(path string, source UserSource) (users []*User) { + contents, err := ioutil.ReadFile(path) + if os.IsNotExist(err) { + return + } if err != nil { log.Fatal(err) } @@ -28,12 +35,23 @@ func usersFromFixedCredentials() []*User { log.Fatal(err) } for _, u := range users { - u.Source = SourceFixed + u.Source = source } + return users +} + +func usersFromFixedCredentials() (users []*User) { + users = usersFromFile(util.FixedCredentialsPath, SourceFixed) log.Println("Found", len(users), "fixed users") return users } +func usersFromRegistrationCredentials() (users []*User) { + users = usersFromFile(registrationCredentialsPath(), SourceRegistration) + log.Println("Found", len(users), "registered users") + return users +} + func rememberUsers(uu []*User) { // uu is used to not shadow the `users` in `users.go`. for _, user := range uu { @@ -71,9 +89,49 @@ func tokenStoragePath() string { if strings.HasPrefix(dir, util.WikiDir) { log.Fatal("Error: Wiki storage directory includes private config files") } + log.Println("Path for saving tokens:", dir) return dir } +func registrationCredentialsPath() string { + path := util.RegistrationCredentialsPath + if path == "" { + dir, err := xdg.DataFile("mycorrhiza/registration.json") + if err != nil { + // No error handling, because the program will fail later anyway when trying to read file "" + log.Println("Error: cannot get a file to registration credentials, so no registered users will be saved.") + } else { + path = dir + } + } + return path +} + +func dumpRegistrationCredentials() error { + tmp := []*User{} + + for u := range YieldUsers() { + if u.Source != SourceRegistration { + continue + } + copiedUser := u + copiedUser.Password = "" + tmp = append(tmp, copiedUser) + } + + blob, err := json.Marshal(tmp) + if err != nil { + log.Println(err) + return err + } + err = ioutil.WriteFile(registrationCredentialsPath(), blob, 0644) + if err != nil { + log.Println(err) + return err + } + return nil +} + func dumpTokens() { tmp := make(map[string]string) diff --git a/user/net.go b/user/net.go index c2822af..88ef769 100644 --- a/user/net.go +++ b/user/net.go @@ -60,6 +60,10 @@ func Register(username, password string) error { Source: SourceRegistration, } users.Store(username, &u) + err = dumpRegistrationCredentials() + if err != nil { + return err + } return nil }