2019-05-17 06:28:50 +03:00
|
|
|
// Copyright (c) 2014-present, b3log.org
|
2014-11-17 06:20:35 +03:00
|
|
|
//
|
2014-11-12 18:13:14 +03:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2014-11-17 06:20:35 +03:00
|
|
|
//
|
2018-03-12 07:28:33 +03:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2014-11-17 06:20:35 +03:00
|
|
|
//
|
2014-11-12 18:13:14 +03:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2014-09-17 10:35:48 +04:00
|
|
|
package session
|
|
|
|
|
2014-08-31 14:50:38 +04:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-09-12 13:10:58 +04:00
|
|
|
"net/http"
|
2014-10-29 18:03:03 +03:00
|
|
|
"path/filepath"
|
2014-11-17 05:49:25 +03:00
|
|
|
"runtime"
|
2015-01-17 06:15:34 +03:00
|
|
|
"strings"
|
2014-11-20 06:17:43 +03:00
|
|
|
"sync"
|
2014-11-30 05:18:17 +03:00
|
|
|
"text/template"
|
2014-12-19 10:43:59 +03:00
|
|
|
"time"
|
2014-09-12 13:10:58 +04:00
|
|
|
|
2014-08-31 14:50:38 +04:00
|
|
|
"github.com/b3log/wide/conf"
|
2014-10-29 18:03:03 +03:00
|
|
|
"github.com/b3log/wide/i18n"
|
2014-09-01 16:50:51 +04:00
|
|
|
"github.com/b3log/wide/util"
|
2014-08-31 14:50:38 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-12-08 09:02:39 +03:00
|
|
|
// TODO: i18n
|
|
|
|
|
2015-03-15 07:11:23 +03:00
|
|
|
userExists = "user exists"
|
|
|
|
userCreated = "user created"
|
|
|
|
userCreateError = "user create error"
|
2014-08-31 14:50:38 +04:00
|
|
|
)
|
|
|
|
|
2014-11-20 06:17:43 +03:00
|
|
|
// Exclusive lock for adding user.
|
|
|
|
var addUserMutex sync.Mutex
|
|
|
|
|
2014-12-07 06:07:32 +03:00
|
|
|
// PreferenceHandler handles request of preference page.
|
2014-11-17 05:49:25 +03:00
|
|
|
func PreferenceHandler(w http.ResponseWriter, r *http.Request) {
|
2019-05-16 19:41:52 +03:00
|
|
|
httpSession, _ := HTTPSession.Get(r, CookieName)
|
2014-11-17 05:49:25 +03:00
|
|
|
|
|
|
|
if httpSession.IsNew {
|
2019-05-16 20:41:04 +03:00
|
|
|
http.Redirect(w, r, "/login", http.StatusFound)
|
2014-11-17 05:49:25 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
httpSession.Options.MaxAge = conf.Wide.HTTPSessionMaxAge
|
|
|
|
httpSession.Save(r, w)
|
|
|
|
|
2019-05-16 18:17:25 +03:00
|
|
|
uid := httpSession.Values["uid"].(string)
|
|
|
|
user := conf.GetUser(uid)
|
2014-11-17 05:49:25 +03:00
|
|
|
|
|
|
|
if "GET" == r.Method {
|
2017-03-29 07:56:57 +03:00
|
|
|
tmpLinux := user.GoBuildArgsForLinux
|
|
|
|
tmpWindows := user.GoBuildArgsForWindows
|
|
|
|
tmpDarwin := user.GoBuildArgsForDarwin
|
2017-05-04 13:05:01 +03:00
|
|
|
|
2017-03-29 07:37:09 +03:00
|
|
|
user.GoBuildArgsForLinux = strings.Replace(user.GoBuildArgsForLinux, `"`, `"`, -1)
|
|
|
|
user.GoBuildArgsForWindows = strings.Replace(user.GoBuildArgsForWindows, `"`, `"`, -1)
|
|
|
|
user.GoBuildArgsForDarwin = strings.Replace(user.GoBuildArgsForDarwin, `"`, `"`, -1)
|
2017-05-04 13:05:01 +03:00
|
|
|
|
2014-11-17 05:49:25 +03:00
|
|
|
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(user.Locale), "user": user,
|
2014-11-26 06:24:24 +03:00
|
|
|
"ver": conf.WideVersion, "goos": runtime.GOOS, "goarch": runtime.GOARCH, "gover": runtime.Version(),
|
2014-11-30 05:18:17 +03:00
|
|
|
"locales": i18n.GetLocalesNames(), "gofmts": util.Go.GetGoFormats(),
|
|
|
|
"themes": conf.GetThemes(), "editorThemes": conf.GetEditorThemes()}
|
2014-11-17 06:20:35 +03:00
|
|
|
|
2014-11-17 05:49:25 +03:00
|
|
|
t, err := template.ParseFiles("views/preference.html")
|
|
|
|
|
|
|
|
if nil != err {
|
2014-12-13 13:47:41 +03:00
|
|
|
logger.Error(err)
|
2019-05-17 05:34:09 +03:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2017-05-04 13:05:01 +03:00
|
|
|
|
2017-03-29 07:56:57 +03:00
|
|
|
user.GoBuildArgsForLinux = tmpLinux
|
|
|
|
user.GoBuildArgsForWindows = tmpWindows
|
|
|
|
user.GoBuildArgsForDarwin = tmpDarwin
|
2014-11-17 05:49:25 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Execute(w, model)
|
2017-05-04 13:05:01 +03:00
|
|
|
|
2017-03-29 07:56:57 +03:00
|
|
|
user.GoBuildArgsForLinux = tmpLinux
|
|
|
|
user.GoBuildArgsForWindows = tmpWindows
|
|
|
|
user.GoBuildArgsForDarwin = tmpDarwin
|
2014-11-17 06:20:35 +03:00
|
|
|
|
2014-11-17 05:49:25 +03:00
|
|
|
return
|
|
|
|
}
|
2014-11-17 06:20:35 +03:00
|
|
|
|
2014-11-17 05:49:25 +03:00
|
|
|
// non-GET request as save request
|
2014-11-17 06:20:35 +03:00
|
|
|
|
2015-11-24 12:39:35 +03:00
|
|
|
result := util.NewResult()
|
|
|
|
defer util.RetResult(w, r, result)
|
2014-11-17 06:20:35 +03:00
|
|
|
|
|
|
|
args := struct {
|
2017-03-27 16:31:35 +03:00
|
|
|
FontFamily string
|
|
|
|
FontSize string
|
|
|
|
GoFmt string
|
|
|
|
GoBuildArgsForLinux string
|
|
|
|
GoBuildArgsForWindows string
|
|
|
|
GoBuildArgsForDarwin string
|
|
|
|
Keymap string
|
|
|
|
Workspace string
|
|
|
|
Username string
|
|
|
|
Locale string
|
|
|
|
Theme string
|
|
|
|
EditorFontFamily string
|
|
|
|
EditorFontSize string
|
|
|
|
EditorLineHeight string
|
|
|
|
EditorTheme string
|
|
|
|
EditorTabSize string
|
2014-11-17 06:20:35 +03:00
|
|
|
}{}
|
|
|
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
|
2014-12-13 13:47:41 +03:00
|
|
|
logger.Error(err)
|
2015-11-24 12:39:35 +03:00
|
|
|
result.Succ = false
|
2014-11-17 06:20:35 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user.FontFamily = args.FontFamily
|
|
|
|
user.FontSize = args.FontSize
|
|
|
|
user.GoFormat = args.GoFmt
|
2017-03-27 16:31:35 +03:00
|
|
|
user.GoBuildArgsForLinux = args.GoBuildArgsForLinux
|
|
|
|
user.GoBuildArgsForWindows = args.GoBuildArgsForWindows
|
|
|
|
user.GoBuildArgsForDarwin = args.GoBuildArgsForDarwin
|
2015-05-11 10:46:17 +03:00
|
|
|
user.Keymap = args.Keymap
|
2015-01-09 05:18:24 +03:00
|
|
|
// XXX: disallow change workspace at present
|
|
|
|
// user.Workspace = args.Workspace
|
2014-12-14 07:27:35 +03:00
|
|
|
|
2014-11-17 06:56:26 +03:00
|
|
|
user.Locale = args.Locale
|
2014-11-30 05:18:17 +03:00
|
|
|
user.Theme = args.Theme
|
|
|
|
user.Editor.FontFamily = args.EditorFontFamily
|
|
|
|
user.Editor.FontSize = args.EditorFontSize
|
|
|
|
user.Editor.LineHeight = args.EditorLineHeight
|
|
|
|
user.Editor.Theme = args.EditorTheme
|
2014-12-01 09:49:16 +03:00
|
|
|
user.Editor.TabSize = args.EditorTabSize
|
2014-11-17 06:20:35 +03:00
|
|
|
|
2019-05-16 18:17:25 +03:00
|
|
|
conf.UpdateCustomizedConf(uid)
|
2014-11-30 05:18:17 +03:00
|
|
|
|
2014-12-23 19:14:03 +03:00
|
|
|
now := time.Now().UnixNano()
|
|
|
|
user.Lived = now
|
|
|
|
user.Updated = now
|
|
|
|
|
2015-11-24 12:39:35 +03:00
|
|
|
result.Succ = user.Save()
|
2014-11-17 05:49:25 +03:00
|
|
|
}
|
|
|
|
|
2014-12-19 10:43:59 +03:00
|
|
|
// FixedTimeSave saves online users' configurations periodically (1 minute).
|
|
|
|
//
|
|
|
|
// Main goal of this function is to save user session content, for restoring session content while user open Wide next time.
|
|
|
|
func FixedTimeSave() {
|
|
|
|
go func() {
|
2015-03-16 06:24:55 +03:00
|
|
|
defer util.Recover()
|
|
|
|
|
2014-12-19 10:43:59 +03:00
|
|
|
for _ = range time.Tick(time.Minute) {
|
2017-05-04 13:05:01 +03:00
|
|
|
SaveOnlineUsers()
|
2014-12-19 10:43:59 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2019-05-16 18:17:25 +03:00
|
|
|
// CanAccess determines whether the user specified by the given user id can access the specified path.
|
|
|
|
func CanAccess(userId, path string) bool {
|
2015-07-23 11:31:37 +03:00
|
|
|
path = filepath.FromSlash(path)
|
|
|
|
|
2019-05-16 18:17:25 +03:00
|
|
|
userWorkspace := conf.GetUserWorkspace(userId)
|
2015-07-23 11:31:37 +03:00
|
|
|
workspaces := filepath.SplitList(userWorkspace)
|
|
|
|
|
|
|
|
for _, workspace := range workspaces {
|
|
|
|
if strings.HasPrefix(path, workspace) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-05-04 13:05:01 +03:00
|
|
|
// SaveOnlineUsers saves online users' configurations at once.
|
|
|
|
func SaveOnlineUsers() {
|
|
|
|
users := getOnlineUsers()
|
|
|
|
for _, u := range users {
|
|
|
|
if u.Save() {
|
|
|
|
logger.Tracef("Saved online user [%s]'s configurations", u.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-19 10:43:59 +03:00
|
|
|
func getOnlineUsers() []*conf.User {
|
|
|
|
ret := []*conf.User{}
|
|
|
|
|
2019-05-16 18:17:25 +03:00
|
|
|
uids := map[string]string{} // distinct uid
|
2014-12-19 10:43:59 +03:00
|
|
|
for _, s := range WideSessions {
|
2019-05-16 18:17:25 +03:00
|
|
|
uids[s.UserId] = s.UserId
|
2014-12-19 10:43:59 +03:00
|
|
|
}
|
|
|
|
|
2019-05-16 18:17:25 +03:00
|
|
|
for _, uid := range uids {
|
|
|
|
u := conf.GetUser(uid)
|
2014-12-19 10:43:59 +03:00
|
|
|
|
2019-05-16 18:17:25 +03:00
|
|
|
if "playground" == uid { // user [playground] is a reserved mock user
|
2015-02-13 04:59:51 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-12-19 10:43:59 +03:00
|
|
|
if nil == u {
|
2019-05-16 18:17:25 +03:00
|
|
|
logger.Warnf("Not found user [%s]", uid)
|
2014-12-19 10:43:59 +03:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = append(ret, u)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|