Skip to content

Commit

Permalink
🏗️ b3log#347
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed May 16, 2019
1 parent 912c582 commit b5aa4f9
Show file tree
Hide file tree
Showing 26 changed files with 348 additions and 394 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/wide.exe
/wide

/static/user/admin/style.css

/header
/header.exe

Expand Down
60 changes: 19 additions & 41 deletions conf/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@
package conf

import (
"crypto/md5"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"time"

"github.com/b3log/wide/util"
)

// Panel represents a UI panel.
Expand All @@ -52,11 +47,9 @@ type LatestSessionContent struct {

// User configuration.
type User struct {
Id string
Name string
Password string
Salt string
Email string
Gravatar string // see http://gravatar.com
Avatar string
Workspace string // the GOPATH of this user (maybe contain several paths splitted by os.PathListSeparator)
Locale string
GoFormat string
Expand All @@ -83,27 +76,6 @@ type editor struct {
TabSize string
}

// NewUser creates a user with the specified username, password, email and workspace.
func NewUser(username, password, email, workspace string) *User {
md5hash := md5.New()
md5hash.Write([]byte(email))
gravatar := hex.EncodeToString(md5hash.Sum(nil))

salt := util.Rand.String(16)
password = Salt(password, salt)

now := time.Now().UnixNano()

return &User{Name: username, Password: password, Salt: salt, Email: email, Gravatar: gravatar, Workspace: workspace,
Locale: Wide.Locale, GoFormat: "gofmt",
GoBuildArgsForLinux: "-i", GoBuildArgsForWindows: "-i", GoBuildArgsForDarwin: "-i",
FontFamily: "Helvetica", FontSize: "13px", Theme: "default",
Keymap: "wide",
Created: now, Updated: now, Lived: now,
Editor: &editor{FontFamily: "Consolas, 'Courier New', monospace", FontSize: "inherit", LineHeight: "17px",
Theme: "wide", TabSize: "4"}}
}

// Save saves the user's configurations in conf/users/{username}.json.
func (u *User) Save() bool {
bytes, err := json.MarshalIndent(u, "", " ")
Expand All @@ -115,12 +87,12 @@ func (u *User) Save() bool {
}

if "" == string(bytes) {
logger.Error("Truncated user [" + u.Name + "]")
logger.Error("Truncated user [" + u.Id + "]")

return false
}

if err = ioutil.WriteFile(filepath.Join(Wide.Users, u.Name+".json"), bytes, 0644); nil != err {
if err = ioutil.WriteFile(filepath.Join(Wide.Users, u.Id+".json"), bytes, 0644); nil != err {
logger.Error(err)

return false
Expand All @@ -129,6 +101,20 @@ func (u *User) Save() bool {
return true
}

// NewUser creates a user with the specified username and workspace.
func NewUser(id, name, avatar, workspace string) *User {
now := time.Now().UnixNano()

return &User{Id: id, Name: name, Avatar: avatar, Workspace: workspace,
Locale: Wide.Locale, GoFormat: "gofmt",
GoBuildArgsForLinux: "-i", GoBuildArgsForWindows: "-i", GoBuildArgsForDarwin: "-i",
FontFamily: "Helvetica", FontSize: "13px", Theme: "default",
Keymap: "wide",
Created: now, Updated: now, Lived: now,
Editor: &editor{FontFamily: "Consolas, 'Courier New', monospace", FontSize: "inherit", LineHeight: "17px",
Theme: "wide", TabSize: "4"}}
}

// WorkspacePath gets workspace path of the user.
//
// Compared to the use of Wide.Workspace, this function will be processed as follows:
Expand Down Expand Up @@ -168,17 +154,9 @@ func (u *User) BuildArgs(os string) []string {
func GetOwner(path string) string {
for _, user := range Users {
if strings.HasPrefix(path, user.WorkspacePath()) {
return user.Name
return user.Id
}
}

return ""
}

// Salt salts the specified password with the specified salt.
func Salt(password, salt string) string {
sha1hash := sha1.New()
sha1hash.Write([]byte(password + salt))

return hex.EncodeToString(sha1hash.Sum(nil))
}
55 changes: 0 additions & 55 deletions conf/users/admin.json

This file was deleted.

33 changes: 17 additions & 16 deletions conf/wide.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ const (
WideVersion = "1.5.3"
// CodeMirrorVer holds the current editor version.
CodeMirrorVer = "5.1"
// UserAgent represents HTTP client user agent.
UserAgent = "Wide/" + WideVersion + "; +https://github.com/b3log/wide"

HelloWorld = `package main
Expand Down Expand Up @@ -70,7 +72,7 @@ type conf struct {
Locale string // default locale
Playground string // playground directory
Users string // users directory
UsersWorkspaces string // users' workspaces directory (admin defaults to ${GOPATH}, others using this)
UsersWorkspaces string // users' workspaces directory
AllowRegister bool // allow register or not
Autocomplete bool // default autocomplete
}
Expand Down Expand Up @@ -335,10 +337,10 @@ func checkEnv() {
}
}

// GetUserWorkspace gets workspace path with the specified username, returns "" if not found.
func GetUserWorkspace(username string) string {
// GetUserWorkspace gets workspace path with the specified user id, returns "" if not found.
func GetUserWorkspace(userId string) string {
for _, user := range Users {
if user.Name == username {
if user.Id == userId {
return user.WorkspacePath()
}
}
Expand All @@ -347,9 +349,9 @@ func GetUserWorkspace(username string) string {
}

// GetGoFmt gets the path of Go format tool, returns "gofmt" if not found "goimports".
func GetGoFmt(username string) string {
func GetGoFmt(userId string) string {
for _, user := range Users {
if user.Name == username {
if user.Id == userId {
switch user.GoFormat {
case "gofmt":
return "gofmt"
Expand All @@ -365,15 +367,14 @@ func GetGoFmt(username string) string {
return "gofmt"
}

// GetUser gets configuration of the user specified by the given username, returns nil if not found.
func GetUser(username string) *User {
if "playground" == username { // reserved user for Playground
// mock it
return NewUser("playground", "", "", "")
// GetUser gets configuration of the user specified by the given user id, returns nil if not found.
func GetUser(id string) *User {
if "playground" == id { // reserved user for Playground
return NewUser("playground", "playground", "", "")
}

for _, user := range Users {
if user.Name == username {
if user.Id == id {
return user
}
}
Expand All @@ -384,17 +385,17 @@ func GetUser(username string) *User {
// initCustomizedConfs initializes the user customized configurations.
func initCustomizedConfs() {
for _, user := range Users {
UpdateCustomizedConf(user.Name)
UpdateCustomizedConf(user.Id)
}
}

// UpdateCustomizedConf creates (if not exists) or updates user customized configuration files.
//
// 1. /static/user/{username}/style.css
func UpdateCustomizedConf(username string) {
func UpdateCustomizedConf(userId string) {
var u *User
for _, user := range Users { // maybe it is a beauty of the trade-off of the another world between design and implementation
if user.Name == username {
if user.Id == userId {
u = user
}
}
Expand All @@ -413,7 +414,7 @@ func UpdateCustomizedConf(username string) {
}

wd := util.OS.Pwd()
dir := filepath.Clean(wd + "/static/user/" + u.Name)
dir := filepath.Clean(wd + "/static/user/" + u.Id)
if err := os.MkdirAll(dir, 0755); nil != err {
logger.Error(err)

Expand Down
20 changes: 10 additions & 10 deletions editor/editors.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func AutocompleteHandler(w http.ResponseWriter, r *http.Request) {

return
}
username := session.Values["username"].(string)
uid := session.Values["uid"].(string)

path := args["path"].(string)

Expand Down Expand Up @@ -147,7 +147,7 @@ func AutocompleteHandler(w http.ResponseWriter, r *http.Request) {

logger.Tracef("offset: %d", offset)

userWorkspace := conf.GetUserWorkspace(username)
userWorkspace := conf.GetUserWorkspace(uid)
workspaces := filepath.SplitList(userWorkspace)
libPath := ""
for _, workspace := range workspaces {
Expand Down Expand Up @@ -183,7 +183,7 @@ func GetExprInfoHandler(w http.ResponseWriter, r *http.Request) {
defer util.RetResult(w, r, result)

session, _ := session.HTTPSession.Get(r, "wide-session")
username := session.Values["username"].(string)
uid := session.Values["uid"].(string)

var args map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
Expand Down Expand Up @@ -228,7 +228,7 @@ func GetExprInfoHandler(w http.ResponseWriter, r *http.Request) {
cmd := exec.Command(ideStub, argv...)
cmd.Dir = curDir

setCmdEnv(cmd, username)
setCmdEnv(cmd, uid)

output, err := cmd.CombinedOutput()
if nil != err {
Expand Down Expand Up @@ -259,7 +259,7 @@ func FindDeclarationHandler(w http.ResponseWriter, r *http.Request) {

return
}
username := session.Values["username"].(string)
uid := session.Values["uid"].(string)

var args map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
Expand Down Expand Up @@ -304,7 +304,7 @@ func FindDeclarationHandler(w http.ResponseWriter, r *http.Request) {
cmd := exec.Command(ideStub, argv...)
cmd.Dir = curDir

setCmdEnv(cmd, username)
setCmdEnv(cmd, uid)

output, err := cmd.CombinedOutput()
if nil != err {
Expand Down Expand Up @@ -347,7 +347,7 @@ func FindUsagesHandler(w http.ResponseWriter, r *http.Request) {

return
}
username := session.Values["username"].(string)
uid := session.Values["uid"].(string)

var args map[string]interface{}

Expand Down Expand Up @@ -392,7 +392,7 @@ func FindUsagesHandler(w http.ResponseWriter, r *http.Request) {
cmd := exec.Command(ideStub, argv...)
cmd.Dir = curDir

setCmdEnv(cmd, username)
setCmdEnv(cmd, uid)

output, err := cmd.CombinedOutput()
if nil != err {
Expand Down Expand Up @@ -453,8 +453,8 @@ func getCursorOffset(code string, line, ch int) (offset int) {
return offset
}

func setCmdEnv(cmd *exec.Cmd, username string) {
userWorkspace := conf.GetUserWorkspace(username)
func setCmdEnv(cmd *exec.Cmd, userId string) {
userWorkspace := conf.GetUserWorkspace(userId)

cmd.Env = append(cmd.Env,
"GOPATH="+userWorkspace,
Expand Down
Loading

0 comments on commit b5aa4f9

Please sign in to comment.