2016-12-02 07:38:54 +00:00
|
|
|
package main
|
|
|
|
import "log"
|
|
|
|
import "strconv"
|
|
|
|
import "net/http"
|
|
|
|
import "golang.org/x/crypto/bcrypt"
|
|
|
|
import "database/sql"
|
|
|
|
import _ "github.com/go-sql-driver/mysql"
|
|
|
|
|
|
|
|
type User struct
|
|
|
|
{
|
|
|
|
ID int
|
|
|
|
Name string
|
|
|
|
Group int
|
|
|
|
Is_Admin bool
|
2016-12-02 08:07:56 +00:00
|
|
|
Is_Super_Admin bool
|
2016-12-04 10:44:28 +00:00
|
|
|
Is_Banned bool
|
2016-12-02 07:38:54 +00:00
|
|
|
Session string
|
|
|
|
Loggedin bool
|
2016-12-02 15:03:31 +00:00
|
|
|
Avatar string
|
2016-12-02 07:38:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func SetPassword(uid int, password string) (error) {
|
|
|
|
salt, err := GenerateSafeString(saltLength)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
password = password + salt
|
|
|
|
hashed_password, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = set_password_stmt.Exec(string(hashed_password), salt, uid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SessionCheck(w http.ResponseWriter, r *http.Request) (User) {
|
2016-12-04 10:44:28 +00:00
|
|
|
user := User{0,"",0,false,false,false,"",false,""}
|
2016-12-02 07:38:54 +00:00
|
|
|
var err error
|
|
|
|
var cookie *http.Cookie
|
|
|
|
|
|
|
|
// Are there any session cookies..?
|
|
|
|
// Assign it to user.name to avoid having to create a temporary variable for the type conversion
|
|
|
|
cookie, err = r.Cookie("uid")
|
|
|
|
if err != nil {
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
user.Name = cookie.Value
|
|
|
|
user.ID, err = strconv.Atoi(user.Name)
|
|
|
|
if err != nil {
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
cookie, err = r.Cookie("session")
|
|
|
|
if err != nil {
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
user.Session = cookie.Value
|
2016-12-03 04:50:35 +00:00
|
|
|
//log.Print("ID: " + user.Name)
|
|
|
|
//log.Print("Session: " + user.Session)
|
2016-12-02 07:38:54 +00:00
|
|
|
|
|
|
|
// Is this session valid..?
|
2016-12-02 15:03:31 +00:00
|
|
|
err = get_session_stmt.QueryRow(user.ID,user.Session).Scan(&user.ID, &user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Avatar)
|
2016-12-02 07:38:54 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return user
|
|
|
|
} else if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return user
|
|
|
|
}
|
2016-12-04 10:44:28 +00:00
|
|
|
user.Is_Admin = (user.Is_Super_Admin || groups[user.Group].Is_Admin)
|
|
|
|
user.Is_Banned = groups[user.Group].Is_Banned
|
2016-12-03 04:50:35 +00:00
|
|
|
if user.Avatar != "" && user.Avatar[0] == '.' {
|
|
|
|
user.Avatar = "/uploads/avatar_" + strconv.Itoa(user.ID) + user.Avatar
|
2016-12-02 15:03:31 +00:00
|
|
|
}
|
2016-12-02 07:38:54 +00:00
|
|
|
user.Loggedin = true
|
2016-12-03 04:50:35 +00:00
|
|
|
/*log.Print("Logged in")
|
2016-12-02 07:38:54 +00:00
|
|
|
log.Print("ID: " + strconv.Itoa(user.ID))
|
|
|
|
log.Print("Group: " + strconv.Itoa(user.Group))
|
|
|
|
log.Print("Name: " + user.Name)
|
|
|
|
if user.Loggedin {
|
|
|
|
log.Print("Loggedin: true")
|
|
|
|
} else {
|
|
|
|
log.Print("Loggedin: false")
|
|
|
|
}
|
|
|
|
if user.Is_Admin {
|
|
|
|
log.Print("Is_Admin: true")
|
|
|
|
} else {
|
|
|
|
log.Print("Is_Admin: false")
|
2016-12-03 04:50:35 +00:00
|
|
|
}*/
|
2016-12-02 07:38:54 +00:00
|
|
|
return user
|
|
|
|
}
|