Add a user page to change password

This commit is contained in:
Chteufleur 2015-10-20 23:39:35 +02:00
parent 4f0f2a6006
commit 32df6b8690
21 changed files with 463 additions and 340 deletions

View File

@ -12,4 +12,4 @@ SessionProvider = memory
SessionGCMaxLifetime = 3600
SessionHashFunc = sha1
SessionHashKey = chucknorriswillkickyourassandeatyoursoul
SessionCookieLifeTime = 60
SessionCookieLifeTime = 3600

View File

@ -6,9 +6,9 @@ import (
"github.com/astaxie/beego"
"datahouse/models/sensor"
"datahouse/models/temperature"
temperatureTmp "datahouse/models/temperature/temp"
"datahouse/models/sensor"
"datahouse/models/variables"
)
@ -42,9 +42,9 @@ func (c *AddTempController) Get() {
func addToTempBdd(snsor *sensor.SensorTable, val int) {
nowUTC := time.Now().UTC()
tempTmp := temperatureTmp.GetTemp(snsor.Id)
if tempTmp == nil || tempTmp.Id == 0 { // Add new entry for sensor
if tempTmp == nil || tempTmp.Id == 0 { // Add new entry for sensor
temperatureTmp.AddData(snsor.Id, int64(val))
} else { // Update entry for sensor
} else { // Update entry for sensor
temperatureTmp.UpdateTemp(snsor.Id, int64(val), nowUTC)
}
}

View File

@ -15,13 +15,12 @@ var (
_, timezoneOffset = time.Now().Zone()
)
type SensorPrint struct {
Name string
Mac string
Value string
Unit string
Horodate string
Name string
Mac string
Value string
Unit string
Horodate string
}
type MainController struct {
@ -37,8 +36,7 @@ func (c *MainController) Get() {
c.TplNames = "index.tpl"
}
func getLastTemps() ([]SensorPrint) {
func getLastTemps() []SensorPrint {
sensors := sensor.GetAllSensor()
var values []SensorPrint
for _, s := range sensors {
@ -55,17 +53,17 @@ func getLastTemps() ([]SensorPrint) {
sens.Unit = "°C"
horodatTmp := t.HorodateGMT
horodatTmp = horodatTmp.Add(time.Duration(timezoneOffset)*time.Second)
horodatTmp = horodatTmp.Add(time.Duration(timezoneOffset) * time.Second)
h, m, _ := horodatTmp.Clock()
hStr := strconv.Itoa(h)
if h < 10 {
hStr = "0"+hStr
hStr = "0" + hStr
}
mStr := strconv.Itoa(m)
if m < 10 {
mStr = "0"+mStr
mStr = "0" + mStr
}
sens.Horodate = hStr+"h"+mStr
sens.Horodate = hStr + "h" + mStr
values = append(values, *sens)
}

View File

@ -3,8 +3,8 @@ package controllers
import (
"github.com/astaxie/beego"
"datahouse/models/user"
"datahouse/models/variables"
"datahouse/models/user"
"datahouse/models/variables"
)
type LoginController struct {
@ -15,38 +15,35 @@ func (c *LoginController) Prepare() {
}
func (c *LoginController) Get() {
sess := c.GetSession(variables.SessionName)
sess := c.GetSession(variables.SessionName)
if sess != nil {
c.Redirect(variables.RootRoute, 302)
return
c.Redirect(variables.UserRoute, 302)
return
}
c.TplNames = "login.tpl"
}
func (c *LoginController) Post() {
sess := c.GetSession(variables.SessionName)
sess := c.GetSession(variables.SessionName)
if sess != nil {
c.Redirect(variables.RootRoute, 302)
return
}
login := c.GetString("login")
login := c.GetString("login")
passwd := c.GetString("password")
if !isLoginOK(login, passwd) {
if !isLoginOK(login, passwd) {
c.Abort("403")
}
c.SetSession(variables.SessionName, login)
c.Redirect(variables.RootRoute, 302)
c.Redirect(variables.UserRoute, 302)
}
func isLoginOK(lgn, pwd string) (bool) {
func isLoginOK(lgn, pwd string) bool {
ret := pwd != "" // Do not authorize empty password
usr := user.GetUserByLogin(lgn)
usr := user.GetUserByLogin(lgn)
return ret && pwd == usr.Password
}

View File

@ -7,8 +7,8 @@ import (
"datahouse/models/temperature"
"datahouse/models/variables"
"strconv"
"fmt"
"strconv"
)
type SensorsController struct {
@ -18,14 +18,13 @@ type SensorsController struct {
func (c *SensorsController) Prepare() {
sess := c.GetSession(variables.SessionName)
if sess == nil {
c.Redirect(variables.LoginRoute, 302)
c.Redirect(variables.LoginRoute, 302)
}
c.Data["IsSensor"] = true
c.Data["version"] = variables.Version
}
func (c *SensorsController) Get() {
sensors := sensor.GetAllSensor()
@ -33,7 +32,6 @@ func (c *SensorsController) Get() {
c.TplNames = "sensors.tpl"
}
func (c *SensorsController) Post() {
description := c.Input().Get("description")
mac := c.Input().Get("mac")

41
controllers/user.go Normal file
View File

@ -0,0 +1,41 @@
package controllers
import (
"github.com/astaxie/beego"
"datahouse/models/user"
"datahouse/models/variables"
)
type UserController struct {
beego.Controller
}
func (c *UserController) Prepare() {
sess := c.GetSession(variables.SessionName)
if sess == nil {
c.Redirect(variables.LoginRoute, 302)
}
// c.Data["IsSensor"] = true
c.Data["version"] = variables.Version
}
func (c *UserController) Get() {
c.TplNames = "user.tpl"
}
func (c *UserController) Post() {
pwd1 := c.Input().Get("password1")
pwd2 := c.Input().Get("password2")
if pwd1 == pwd2 {
login := c.GetSession(variables.SessionName)
switch lo := login.(type) {
case string:
user.ChangePassword(lo, pwd1)
}
}
c.Redirect(variables.UserRoute, 302)
}

View File

@ -3,16 +3,14 @@ package controllers
import (
"github.com/astaxie/beego"
"datahouse/models/temperature"
"datahouse/models/sensor"
"datahouse/models/temperature"
"datahouse/models/variables"
"html/template"
"strconv"
)
type ViewTempController struct {
beego.Controller
}
@ -25,7 +23,7 @@ func (c *ViewTempController) Prepare() {
func (c *ViewTempController) Get() {
dataTemp := ""
sensors := sensor.GetAllSensor()
for i := 0; i<len(sensors); i++ {
for i := 0; i < len(sensors); i++ {
if i > 0 {
dataTemp += ","
}
@ -41,21 +39,19 @@ func (c *ViewTempController) Get() {
c.TplNames = "temp.tpl"
}
/*
--------------------------------------------------------------------------------
*/
func formatDataSensor(sensorName string, values []temperature.TempTable) string {
ret := "{name : \""+sensorName+"\", data : ["
for i := 0; i<len(values); i++ {
ret := "{name : \"" + sensorName + "\", data : ["
for i := 0; i < len(values); i++ {
if i > 0 {
ret += ","
}
horodate := strconv.FormatInt((values[i].HorodateGMT.Unix()+int64(timezoneOffset)) * 1000, 10)
horodate := strconv.FormatInt((values[i].HorodateGMT.Unix()+int64(timezoneOffset))*1000, 10)
value := strconv.FormatInt(values[i].Value, 10)
ret += "["+horodate+","+value+"]"
ret += "[" + horodate + "," + value + "]"
}
ret += "]}"
return ret

10
main.go
View File

@ -1,10 +1,10 @@
package main
import (
_ "datahouse/routers"
"datahouse/models/database"
_ "datahouse/models/temperature"
"datahouse/models/user"
_ "datahouse/routers"
"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
@ -20,17 +20,17 @@ func init() {
log.SetLogger("console", "")
orm.RegisterDriver("mysql", orm.DR_MySQL)
url := database.UserDB+":"+database.PwdDB+"@/"+database.DataBase+"?charset=utf8"
url := database.UserDB + ":" + database.PwdDB + "@/" + database.DataBase + "?charset=utf8"
err := orm.RegisterDataBase(database.Alias, "mysql", url)
if err != nil {
log.Error("Failed to register database", err)
log.Error("Failed to register database", err)
}
force := false
verbose := true
err = orm.RunSyncdb(database.Alias, force, verbose)
if err != nil {
log.Error("Failed to initialize database", err)
log.Error("Failed to initialize database", err)
}
}
@ -38,7 +38,7 @@ func main() {
if !user.IsUserExist("admin") {
user.AddUser("admin", "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918")
}
}
beego.Run()
}

View File

@ -1,13 +1,13 @@
package database
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego"
)
var (
Alias = "default"
UserDB = beego.AppConfig.String("mysqluser")
PwdDB = beego.AppConfig.String("mysqlpass")
HostDB = beego.AppConfig.String("mysqlurls")
DataBase = beego.AppConfig.String("mysqldb")
Alias = "default"
UserDB = beego.AppConfig.String("mysqluser")
PwdDB = beego.AppConfig.String("mysqlpass")
HostDB = beego.AppConfig.String("mysqlurls")
DataBase = beego.AppConfig.String("mysqldb")
)

View File

@ -1,131 +1,127 @@
package sensor
import (
"github.com/astaxie/beego/orm"
"github.com/astaxie/beego/orm"
"datahouse/models/database"
"datahouse/models/utils"
"datahouse/models/database"
"datahouse/models/utils"
)
type SensorTable struct {
Id int64
SensorMAC string
Description string
Interval int64
Id int64
SensorMAC string
Description string
Interval int64
}
func init() {
orm.RegisterModel(new(SensorTable))
orm.RegisterModel(new(SensorTable))
}
func GetSensorByMac(sensorMac string) *SensorTable {
o := orm.NewOrm()
o.Using(database.Alias)
func GetSensorByMac(sensorMac string) (*SensorTable) {
o := orm.NewOrm()
o.Using(database.Alias)
sensor := new(SensorTable)
ret := new(SensorTable)
var maps []orm.Params
_, err := o.QueryTable(sensor).Filter("SensorMAC", sensorMac).Values(&maps)
if err == nil {
for _, m := range maps {
ret.Id = utils.GetInt(m, "Id")
ret.SensorMAC = utils.GetString(m, "SensorMAC")
ret.Description = utils.GetString(m, "Description")
ret.Interval = utils.GetInt(m, "Interval")
break
}
sensor := new(SensorTable)
ret := new(SensorTable)
var maps []orm.Params
_, err := o.QueryTable(sensor).Filter("SensorMAC", sensorMac).Values(&maps)
if err == nil {
for _, m := range maps {
ret.Id = utils.GetInt(m, "Id")
ret.SensorMAC = utils.GetString(m, "SensorMAC")
ret.Description = utils.GetString(m, "Description")
ret.Interval = utils.GetInt(m, "Interval")
break
}
}
return ret
return ret
}
func GetAllSensorIds() ([]int64) {
o := orm.NewOrm()
o.Using(database.Alias)
func GetAllSensorIds() []int64 {
o := orm.NewOrm()
o.Using(database.Alias)
sensor := new(SensorTable)
var ret []int64
var maps []orm.Params
_, err := o.QueryTable(sensor).Values(&maps)
if err == nil {
for _, m := range maps {
ret = append(ret, utils.GetInt(m, "Id"))
}
}
return ret
sensor := new(SensorTable)
var ret []int64
var maps []orm.Params
_, err := o.QueryTable(sensor).Values(&maps)
if err == nil {
for _, m := range maps {
ret = append(ret, utils.GetInt(m, "Id"))
}
}
return ret
}
func GetAllSensor() ([]SensorTable) {
o := orm.NewOrm()
o.Using(database.Alias)
func GetAllSensor() []SensorTable {
o := orm.NewOrm()
o.Using(database.Alias)
sensor := new(SensorTable)
var ret []SensorTable
var maps []orm.Params
_, err := o.QueryTable(sensor).Values(&maps)
if err == nil {
for _, m := range maps {
r := new(SensorTable)
r.Id = utils.GetInt(m, "Id")
r.SensorMAC = utils.GetString(m, "SensorMAC")
r.Description = utils.GetString(m, "Description")
r.Interval = utils.GetInt(m, "Interval")
sensor := new(SensorTable)
var ret []SensorTable
var maps []orm.Params
_, err := o.QueryTable(sensor).Values(&maps)
if err == nil {
for _, m := range maps {
r := new(SensorTable)
r.Id = utils.GetInt(m, "Id")
r.SensorMAC = utils.GetString(m, "SensorMAC")
r.Description = utils.GetString(m, "Description")
r.Interval = utils.GetInt(m, "Interval")
ret = append(ret, *r)
}
}
return ret
ret = append(ret, *r)
}
}
return ret
}
func GetSensor(id int64) (*SensorTable) {
o := orm.NewOrm()
o.Using(database.Alias)
func GetSensor(id int64) *SensorTable {
o := orm.NewOrm()
o.Using(database.Alias)
sensor := new(SensorTable)
var ret = new(SensorTable)
var maps []orm.Params
_, err := o.QueryTable(sensor).Filter("Id", id).Values(&maps)
if err == nil {
for _, m := range maps {
ret.Id = utils.GetInt(m, "Id")
ret.SensorMAC = utils.GetString(m, "SensorMAC")
ret.Description = utils.GetString(m, "Description")
ret.Interval = utils.GetInt(m, "Interval")
}
}
return ret
sensor := new(SensorTable)
var ret = new(SensorTable)
var maps []orm.Params
_, err := o.QueryTable(sensor).Filter("Id", id).Values(&maps)
if err == nil {
for _, m := range maps {
ret.Id = utils.GetInt(m, "Id")
ret.SensorMAC = utils.GetString(m, "SensorMAC")
ret.Description = utils.GetString(m, "Description")
ret.Interval = utils.GetInt(m, "Interval")
}
}
return ret
}
func UpdateSensor(mac, description string, interval int64) {
o := orm.NewOrm()
o.Using(database.Alias)
o := orm.NewOrm()
o.Using(database.Alias)
s := GetSensorByMac(mac)
if o.Read(s) == nil {
s.Description = description
s.Interval = interval
o.Update(s)
}
s := GetSensorByMac(mac)
if o.Read(s) == nil {
s.Description = description
s.Interval = interval
o.Update(s)
}
}
func AddSensor(sensorMac string) {
o := orm.NewOrm()
o.Using(database.Alias)
_, _ = o.Insert(&SensorTable{SensorMAC: sensorMac})
o := orm.NewOrm()
o.Using(database.Alias)
_, _ = o.Insert(&SensorTable{SensorMAC: sensorMac})
}
func DeleteSensorByMac(sensorMac string) {
o := orm.NewOrm()
o.Using(database.Alias)
o.Delete(&SensorTable{SensorMAC: sensorMac})
o := orm.NewOrm()
o.Using(database.Alias)
o.Delete(&SensorTable{SensorMAC: sensorMac})
}
func DeleteSensor(sensorId int64) {
o := orm.NewOrm()
o.Using(database.Alias)
o.Delete(&SensorTable{Id: sensorId})
o := orm.NewOrm()
o.Using(database.Alias)
o.Delete(&SensorTable{Id: sensorId})
}

View File

@ -1,72 +1,67 @@
package temp
import (
"github.com/astaxie/beego/orm"
"github.com/astaxie/beego/orm"
"datahouse/models/database"
"datahouse/models/utils"
"datahouse/models/database"
"datahouse/models/utils"
"time"
"time"
)
/**
* Store last received data.
*/
type TempTableTmp struct {
Id int64
HorodateGMT time.Time `orm:"auto_now;type(datetime)"`
SensorID int64
Value int64
Id int64
HorodateGMT time.Time `orm:"auto_now;type(datetime)"`
SensorID int64
Value int64
}
func init() {
orm.RegisterModel(new(TempTableTmp))
orm.RegisterModel(new(TempTableTmp))
}
/**
* Add the value into the database.
*/
func AddData(sensor, value int64) {
o := orm.NewOrm()
o.Using(database.Alias)
o := orm.NewOrm()
o.Using(database.Alias)
tmpTable := new(TempTableTmp)
tmpTable.Value = value
tmpTable.SensorID = sensor
o.Insert(tmpTable)
tmpTable := new(TempTableTmp)
tmpTable.Value = value
tmpTable.SensorID = sensor
o.Insert(tmpTable)
}
func GetTemp(sensorId int64) *TempTableTmp {
o := orm.NewOrm()
o.Using(database.Alias)
func GetTemp(sensorId int64) (*TempTableTmp) {
o := orm.NewOrm()
o.Using(database.Alias)
data := new(TempTableTmp)
var maps []orm.Params
_, err := o.QueryTable(new(TempTableTmp)).Filter("SensorID", sensorId).Values(&maps)
if err == nil {
for _, m := range maps {
data.Id = utils.GetInt(m, "Id")
data.HorodateGMT = utils.GetTime(m, "HorodateGMT")
data.SensorID = utils.GetInt(m, "SensorID")
data.Value = utils.GetInt(m, "Value")
}
}
return data
data := new(TempTableTmp)
var maps []orm.Params
_, err := o.QueryTable(new(TempTableTmp)).Filter("SensorID", sensorId).Values(&maps)
if err == nil {
for _, m := range maps {
data.Id = utils.GetInt(m, "Id")
data.HorodateGMT = utils.GetTime(m, "HorodateGMT")
data.SensorID = utils.GetInt(m, "SensorID")
data.Value = utils.GetInt(m, "Value")
}
}
return data
}
func UpdateTemp(sensorId, val int64, date time.Time) {
o := orm.NewOrm()
o.Using(database.Alias)
o := orm.NewOrm()
o.Using(database.Alias)
temp := GetTemp(sensorId)
if o.Read(temp) == nil {
temp.HorodateGMT = date
temp.Value = val
o.Update(temp)
}
temp := GetTemp(sensorId)
if o.Read(temp) == nil {
temp.HorodateGMT = date
temp.Value = val
o.Update(temp)
}
}

View File

@ -1,84 +1,83 @@
package temperature
import (
"github.com/astaxie/beego/orm"
"github.com/astaxie/beego/orm"
"datahouse/models/database"
"datahouse/models/utils"
"datahouse/models/database"
"datahouse/models/utils"
"time"
"time"
)
type TempTable struct {
Id int64
HorodateGMT time.Time `orm:"auto_now;type(datetime)"`
SensorID int64
Value int64
Id int64
HorodateGMT time.Time `orm:"auto_now;type(datetime)"`
SensorID int64
Value int64
}
func init() {
orm.RegisterModel(new(TempTable))
orm.RegisterModel(new(TempTable))
}
/**
* Add the value into the database.
*/
func AddData(sensor, value int64) {
o := orm.NewOrm()
o.Using(database.Alias)
o := orm.NewOrm()
o.Using(database.Alias)
tmpTable := new(TempTable)
tmpTable.Value = value
tmpTable.SensorID = sensor
o.Insert(tmpTable)
tmpTable := new(TempTable)
tmpTable.Value = value
tmpTable.SensorID = sensor
o.Insert(tmpTable)
}
func GetAllTempForSensor(sensorId int64) ([]TempTable) {
o := orm.NewOrm()
o.Using(database.Alias)
func GetAllTempForSensor(sensorId int64) []TempTable {
o := orm.NewOrm()
o.Using(database.Alias)
var dataArray []TempTable
var maps []orm.Params
_, err := o.QueryTable(new(TempTable)).Filter("SensorID", sensorId).Values(&maps)
if err == nil {
for _, m := range maps {
d := new(TempTable)
d.Id = utils.GetInt(m, "Id")
d.HorodateGMT = utils.GetTime(m, "HorodateGMT")
d.SensorID = utils.GetInt(m, "SensorID")
d.Value = utils.GetInt(m, "Value")
var dataArray []TempTable
var maps []orm.Params
_, err := o.QueryTable(new(TempTable)).Filter("SensorID", sensorId).Values(&maps)
if err == nil {
for _, m := range maps {
d := new(TempTable)
d.Id = utils.GetInt(m, "Id")
d.HorodateGMT = utils.GetTime(m, "HorodateGMT")
d.SensorID = utils.GetInt(m, "SensorID")
d.Value = utils.GetInt(m, "Value")
dataArray = append(dataArray, *d)
}
}
return dataArray
dataArray = append(dataArray, *d)
}
}
return dataArray
}
func GetLastTempForSensor(sensorId int64) (*TempTable) {
o := orm.NewOrm()
o.Using(database.Alias)
func GetLastTempForSensor(sensorId int64) *TempTable {
o := orm.NewOrm()
o.Using(database.Alias)
data := new(TempTable)
var maps []orm.Params
_, err := o.QueryTable(new(TempTable)).Filter("SensorID", sensorId).OrderBy("HorodateGMT").Values(&maps)
if err == nil {
for _, m := range maps {
data.Id = utils.GetInt(m, "Id")
data.HorodateGMT = utils.GetTime(m, "HorodateGMT")
data.SensorID = utils.GetInt(m, "SensorID")
data.Value = utils.GetInt(m, "Value")
}
}
return data
data := new(TempTable)
var maps []orm.Params
_, err := o.QueryTable(new(TempTable)).Filter("SensorID", sensorId).OrderBy("HorodateGMT").Values(&maps)
if err == nil {
for _, m := range maps {
data.Id = utils.GetInt(m, "Id")
data.HorodateGMT = utils.GetTime(m, "HorodateGMT")
data.SensorID = utils.GetInt(m, "SensorID")
data.Value = utils.GetInt(m, "Value")
}
}
return data
}
func DeleteTemperatureBySenor(sensorId int64) {
o := orm.NewOrm()
o.Using(database.Alias)
o := orm.NewOrm()
o.Using(database.Alias)
temps := GetAllTempForSensor(sensorId)
for _, temp := range temps {
o.Delete(&TempTable{Id: temp.Id})
}
temps := GetAllTempForSensor(sensorId)
for _, temp := range temps {
o.Delete(&TempTable{Id: temp.Id})
}
}

View File

@ -1,84 +1,90 @@
package user
import (
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
"datahouse/models/database"
"datahouse/models/utils"
"datahouse/models/database"
"datahouse/models/utils"
)
type User struct {
Id int64
Login string
Password string
Id int64
Login string
Password string
}
func init() {
// register model
orm.RegisterModel(new(User))
// register model
orm.RegisterModel(new(User))
}
func IsUserExist(login string) bool {
o := orm.NewOrm()
o.Using(database.Alias)
func IsUserExist(login string) (bool) {
o := orm.NewOrm()
o.Using(database.Alias)
ret := false
var maps []orm.Params
_, err := o.QueryTable(new(User)).Filter("Login", login).Values(&maps)
if err == nil {
for _, _ = range maps {
ret = true
break
}
}
return ret
ret := false
var maps []orm.Params
_, err := o.QueryTable(new(User)).Filter("Login", login).Values(&maps)
if err == nil {
for _, _ = range maps {
ret = true
break
}
}
return ret
}
func GetUserByLogin(login string) (*User) {
o := orm.NewOrm()
o.Using(database.Alias)
func GetUserByLogin(login string) *User {
o := orm.NewOrm()
o.Using(database.Alias)
ret := new(User)
var maps []orm.Params
_, err := o.QueryTable(new(User)).Filter("Login", login).Values(&maps)
if err == nil {
for _, m := range maps {
ret.Id = utils.GetInt(m, "Id")
ret.Login = utils.GetString(m, "Login")
ret.Password = utils.GetString(m, "Password")
break
}
}
return ret
ret := new(User)
var maps []orm.Params
_, err := o.QueryTable(new(User)).Filter("Login", login).Values(&maps)
if err == nil {
for _, m := range maps {
ret.Id = utils.GetInt(m, "Id")
ret.Login = utils.GetString(m, "Login")
ret.Password = utils.GetString(m, "Password")
break
}
}
return ret
}
func GetUser(id int64) *User {
o := orm.NewOrm()
o.Using(database.Alias)
func GetUser(id int64) (*User) {
o := orm.NewOrm()
o.Using(database.Alias)
ret := new(User)
var maps []orm.Params
_, err := o.QueryTable(new(User)).Filter("Id", id).Values(&maps)
if err == nil {
for _, m := range maps {
ret.Id = utils.GetInt(m, "Id")
ret.Login = utils.GetString(m, "Login")
ret.Password = utils.GetString(m, "Password")
break
}
}
return ret
ret := new(User)
var maps []orm.Params
_, err := o.QueryTable(new(User)).Filter("Id", id).Values(&maps)
if err == nil {
for _, m := range maps {
ret.Id = utils.GetInt(m, "Id")
ret.Login = utils.GetString(m, "Login")
ret.Password = utils.GetString(m, "Password")
break
}
}
return ret
}
func AddUser(login, sha256Pass string) {
o := orm.NewOrm()
o.Using(database.Alias)
o := orm.NewOrm()
o.Using(database.Alias)
_, _ = o.Insert(&User{Login: login, Password: sha256Pass})
_, _ = o.Insert(&User{Login: login, Password: sha256Pass})
}
func ChangePassword(login, newPwd string) {
o := orm.NewOrm()
o.Using(database.Alias)
user := GetUserByLogin(login)
if o.Read(user) == nil {
user.Password = newPwd
o.Update(user)
}
}

View File

@ -1,41 +1,40 @@
package utils
import (
"github.com/astaxie/beego/orm"
"github.com/astaxie/beego/orm"
"time"
"time"
)
func GetString(m orm.Params, param string) string {
ret := ""
switch i := m[param].(type) {
case string:
ret = i
}
ret := ""
switch i := m[param].(type) {
case string:
ret = i
}
return ret
return ret
}
func GetInt(m orm.Params, param string) int64 {
var ret int64
ret = -1
var ret int64
ret = -1
switch i := m[param].(type) {
case int, int32, int64:
ret = i.(int64)
}
switch i := m[param].(type) {
case int, int32, int64:
ret = i.(int64)
}
return ret
return ret
}
func GetTime(m orm.Params, param string) time.Time {
var ret time.Time
var ret time.Time
switch i := m[param].(type) {
case time.Time:
ret = i
}
switch i := m[param].(type) {
case time.Time:
ret = i
}
return ret
return ret
}

View File

@ -1,15 +1,16 @@
package variables
const (
Version = "0.0.3"
Version = "0.0.3"
SessionName = "Session_Data_House"
SessionName = "Session_Data_House"
)
var (
RootRoute = "/"
AddTempRoute = "/add/temp/:sensor([0-9A-Fa-f:]+)/:val([0-9]+)"
ViewTempRoute = "/view/temp"
SensorsRoute = "/sensors"
LoginRoute = "/login"
RootRoute = "/"
AddTempRoute = "/add/temp/:sensor([0-9A-Fa-f:]+)/:val([0-9]+)"
ViewTempRoute = "/view/temp"
SensorsRoute = "/sensors"
LoginRoute = "/login"
UserRoute = "/user"
)

View File

@ -7,9 +7,10 @@ import (
)
func init() {
beego.Router(variables.RootRoute, &controllers.MainController{})
beego.Router(variables.AddTempRoute, &controllers.AddTempController{})
beego.Router(variables.ViewTempRoute, &controllers.ViewTempController{})
beego.Router(variables.SensorsRoute, &controllers.SensorsController{})
beego.Router(variables.LoginRoute, &controllers.LoginController{})
beego.Router(variables.RootRoute, &controllers.MainController{})
beego.Router(variables.AddTempRoute, &controllers.AddTempController{})
beego.Router(variables.ViewTempRoute, &controllers.ViewTempController{})
beego.Router(variables.SensorsRoute, &controllers.SensorsController{})
beego.Router(variables.LoginRoute, &controllers.LoginController{})
beego.Router(variables.UserRoute, &controllers.UserController{})
}

40
static/css/user.css Normal file
View File

@ -0,0 +1,40 @@
body {
padding-top: 40px;
padding-bottom: 40px;
/*background-color: #eee;*/
}
.form-signin {
max-width: 450px;
padding: 15px;
margin: 0 auto;
}
/*.form-signin .form-signin-heading,
.form-signin .checkbox {
margin-bottom: 10px;
}
.form-signin .checkbox {
font-weight: normal;
}*/
.form-signin .form-control {
position: relative;
height: auto;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
padding: 10px;
font-size: 16px;
}
.form-signin .form-control:focus {
z-index: 2;
}
/*.form-signin input[type="email"] {
margin-bottom: -1px;
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
}*/
.form-signin input[type="password"] {
margin-bottom: 10px;
border-top-left-radius: 0;
border-top-right-radius: 0;
}

View File

@ -14,3 +14,5 @@
<script type="text/javascript" src="/static/bootstrap/js/npm.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="../../assets/js/ie10-viewport-bug-workaround.js"></script>
{{template "extrajs" .}}

View File

@ -14,7 +14,17 @@
<div class="form-group">
<label>Interval de prise de mesure : </label>
<input type="number" class="form-control" name="interval" value="{{$val.Interval}}"/>
<label>secondes</label>
<!-- <select name="interval">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select> -->
</div>
<br/><br/>
<div class="form-group">

View File

@ -25,7 +25,19 @@
}, {
count: 1,
type: 'day',
text: '24h'
text: '1j'
}, {
count: 2,
type: 'day',
text: '2j'
}, {
count: 3,
type: 'day',
text: '3j'
}, {
count: 5,
type: 'day',
text: '5j'
}, {
count: 7,
type: 'day',

32
views/user.tpl Normal file
View File

@ -0,0 +1,32 @@
{{template "base/base.html" .}}
{{define "meta"}}
<link href="/static/css/user.css" rel="stylesheet">
{{end}}
{{define "body"}}
<center><h2 class="form-signin-heading">Chagement de mot de passe</h2></center>
<form id="chPwdForm" class="form-signin" action="/user" method="POST">
<label for="inputEmail" class="sr-only">Mot de passe</label>
<input type="password" id="password1" name="password1" class="form-control" placeholder="Password" required>
<label for="inputPassword" class="sr-only">Mot de passe</label>
<input type="password" id="password2" name="password2" class="form-control" placeholder="Password" required>
<button class="btn btn-lg btn-primary btn-block" type="submit">Enregistrer</button>
</form>
{{end}}
{{define "extrajs"}}
<script src="/static/js/jquery.sha256.min.js"></script>
<script type="application/javascript">
$("#chPwdForm").submit(function() {
$('#password1').val($.sha256($('#password1').val()));
console.log("Password1: "+$('#password1').val());
$('#password2').val($.sha256($('#password2').val()));
console.log("Password2: "+$('#password2').val());
return $('#password1').val() == $('#password2').val();
});
</script>
{{end}}