Commits

Dobrosław Żybort committed 0192714

Add User_ID reference to pastes table, and default 'Anonymous' user
Login form: password can't be empty

Comments (0)

Files changed (5)

 
 func insertPaste(paste *Paste, ctx *Context) (lastId int64, err error) {
 	result, err := ctx.Database.Exec(`
-		insert into pastes(Title, Author, Datetime, Data, Protection, Syntax, IP) 
-		values (?, ?, ?, ?, ?,?, ?)`,
+		insert into pastes(Title, Author, User_ID, Datetime, Data, 
+			Protection, Syntax, IP) 
+		values (?, ?, ?, ?, ?,?, ?, ?)`,
 		paste.Title,
 		paste.Author,
+		paste.UserID,
 		paste.Datetime,
 		paste.Data,
 		paste.Protection,

databaseInitSqlite.go

 			Datetime datetime, 
 			Title varchar(255) DEFAULT 'Untitled', 
 			Author varchar(255) DEFAULT 'Anonymous', 
+			User_ID integer DEFAULT 1, 
 			Protection integer, 
 			Syntax varchar(255) DEFAULT 'plaintext', 
 			URL longtext, 
 			User_ID integer PRIMARY KEY AUTOINCREMENT NOT NULL,
 			Display_name varchar(255) UNIQUE,
 			Password blob(64)
-		)`,
+		)`, `
+		INSERT OR IGNORE INTO users (Display_name) VALUES('Anonymous')
+		`,
 	}
 	for _, sql := range sqls {
 		_, err = con.Exec(sql)
 		}
 	} else {
 		paste.Author = ctx.User.Name
+		paste.UserID = ctx.User.ID
 	}
 	paste.Protection = pasteData.Protection
 	paste.Syntax = pasteData.Syntax
 	userData := new(UserForm)
 	schema.NewDecoder().Decode(userData, req.Form)
 
+	if userData.Password == "" {
+		ctx.Session.AddFlash("Password can't be empty")
+		return loginForm(wri, req, ctx)
+	}
+
 	user, err := userLogin(userData.Name, userData.Password, ctx)
 	if err != nil {
 		ctx.Session.AddFlash("Invalid username/password")
 	Datetime        time.Time `json:"Datetime"`
 	Title           string    `json:"Title"`
 	Author          string    `json:"Author"`
+	UserID          int64     `json:"User_ID"`
 	Protection      int8      `json:"Protection"`
 	Syntax          string    `json:"Syntax"`
 	URL             string    `json:"URL"`
 		Datetime:   time.Now().UTC(),
 		Title:      "Untitled",
 		Author:     "Anonymous",
+		UserID:     1,
 		Syntax:     "plaintext",
 		Protection: 0,
 	}