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 SessionGCMaxLifetime = 3600
SessionHashFunc = sha1 SessionHashFunc = sha1
SessionHashKey = chucknorriswillkickyourassandeatyoursoul SessionHashKey = chucknorriswillkickyourassandeatyoursoul
SessionCookieLifeTime = 60 SessionCookieLifeTime = 3600

View File

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

View File

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

View File

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

View File

@ -7,8 +7,8 @@ import (
"datahouse/models/temperature" "datahouse/models/temperature"
"datahouse/models/variables" "datahouse/models/variables"
"strconv"
"fmt" "fmt"
"strconv"
) )
type SensorsController struct { type SensorsController struct {
@ -18,14 +18,13 @@ type SensorsController struct {
func (c *SensorsController) Prepare() { func (c *SensorsController) Prepare() {
sess := c.GetSession(variables.SessionName) sess := c.GetSession(variables.SessionName)
if sess == nil { if sess == nil {
c.Redirect(variables.LoginRoute, 302) c.Redirect(variables.LoginRoute, 302)
} }
c.Data["IsSensor"] = true c.Data["IsSensor"] = true
c.Data["version"] = variables.Version c.Data["version"] = variables.Version
} }
func (c *SensorsController) Get() { func (c *SensorsController) Get() {
sensors := sensor.GetAllSensor() sensors := sensor.GetAllSensor()
@ -33,7 +32,6 @@ func (c *SensorsController) Get() {
c.TplNames = "sensors.tpl" c.TplNames = "sensors.tpl"
} }
func (c *SensorsController) Post() { func (c *SensorsController) Post() {
description := c.Input().Get("description") description := c.Input().Get("description")
mac := c.Input().Get("mac") 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 ( import (
"github.com/astaxie/beego" "github.com/astaxie/beego"
"datahouse/models/temperature"
"datahouse/models/sensor" "datahouse/models/sensor"
"datahouse/models/temperature"
"datahouse/models/variables" "datahouse/models/variables"
"html/template" "html/template"
"strconv" "strconv"
) )
type ViewTempController struct { type ViewTempController struct {
beego.Controller beego.Controller
} }
@ -25,7 +23,7 @@ func (c *ViewTempController) Prepare() {
func (c *ViewTempController) Get() { func (c *ViewTempController) Get() {
dataTemp := "" dataTemp := ""
sensors := sensor.GetAllSensor() sensors := sensor.GetAllSensor()
for i := 0; i<len(sensors); i++ { for i := 0; i < len(sensors); i++ {
if i > 0 { if i > 0 {
dataTemp += "," dataTemp += ","
} }
@ -41,21 +39,19 @@ func (c *ViewTempController) Get() {
c.TplNames = "temp.tpl" c.TplNames = "temp.tpl"
} }
/* /*
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
*/ */
func formatDataSensor(sensorName string, values []temperature.TempTable) string { func formatDataSensor(sensorName string, values []temperature.TempTable) string {
ret := "{name : \""+sensorName+"\", data : [" ret := "{name : \"" + sensorName + "\", data : ["
for i := 0; i<len(values); i++ { for i := 0; i < len(values); i++ {
if i > 0 { if i > 0 {
ret += "," 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) value := strconv.FormatInt(values[i].Value, 10)
ret += "["+horodate+","+value+"]" ret += "[" + horodate + "," + value + "]"
} }
ret += "]}" ret += "]}"
return ret return ret

10
main.go
View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,84 +1,90 @@
package user package user
import ( import (
"github.com/astaxie/beego/orm" "github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"datahouse/models/database" "datahouse/models/database"
"datahouse/models/utils" "datahouse/models/utils"
) )
type User struct { type User struct {
Id int64 Id int64
Login string Login string
Password string Password string
} }
func init() { func init() {
// register model // register model
orm.RegisterModel(new(User)) orm.RegisterModel(new(User))
} }
func IsUserExist(login string) bool {
o := orm.NewOrm()
o.Using(database.Alias)
func IsUserExist(login string) (bool) { ret := false
o := orm.NewOrm() var maps []orm.Params
o.Using(database.Alias) _, err := o.QueryTable(new(User)).Filter("Login", login).Values(&maps)
if err == nil {
ret := false for _, _ = range maps {
var maps []orm.Params ret = true
_, err := o.QueryTable(new(User)).Filter("Login", login).Values(&maps) break
if err == nil { }
for _, _ = range maps { }
ret = true return ret
break
}
}
return ret
} }
func GetUserByLogin(login string) (*User) { func GetUserByLogin(login string) *User {
o := orm.NewOrm() o := orm.NewOrm()
o.Using(database.Alias) o.Using(database.Alias)
ret := new(User) ret := new(User)
var maps []orm.Params var maps []orm.Params
_, err := o.QueryTable(new(User)).Filter("Login", login).Values(&maps) _, err := o.QueryTable(new(User)).Filter("Login", login).Values(&maps)
if err == nil { if err == nil {
for _, m := range maps { for _, m := range maps {
ret.Id = utils.GetInt(m, "Id") ret.Id = utils.GetInt(m, "Id")
ret.Login = utils.GetString(m, "Login") ret.Login = utils.GetString(m, "Login")
ret.Password = utils.GetString(m, "Password") ret.Password = utils.GetString(m, "Password")
break break
} }
} }
return ret return ret
} }
func GetUser(id int64) *User {
o := orm.NewOrm()
o.Using(database.Alias)
func GetUser(id int64) (*User) { ret := new(User)
o := orm.NewOrm() var maps []orm.Params
o.Using(database.Alias) _, err := o.QueryTable(new(User)).Filter("Id", id).Values(&maps)
if err == nil {
ret := new(User) for _, m := range maps {
var maps []orm.Params ret.Id = utils.GetInt(m, "Id")
_, err := o.QueryTable(new(User)).Filter("Id", id).Values(&maps) ret.Login = utils.GetString(m, "Login")
if err == nil { ret.Password = utils.GetString(m, "Password")
for _, m := range maps { break
ret.Id = utils.GetInt(m, "Id") }
ret.Login = utils.GetString(m, "Login") }
ret.Password = utils.GetString(m, "Password") return ret
break
}
}
return ret
} }
func AddUser(login, sha256Pass string) { func AddUser(login, sha256Pass string) {
o := orm.NewOrm() o := orm.NewOrm()
o.Using(database.Alias) 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 package utils
import ( import (
"github.com/astaxie/beego/orm" "github.com/astaxie/beego/orm"
"time" "time"
) )
func GetString(m orm.Params, param string) string { func GetString(m orm.Params, param string) string {
ret := "" ret := ""
switch i := m[param].(type) { switch i := m[param].(type) {
case string: case string:
ret = i ret = i
} }
return ret return ret
} }
func GetInt(m orm.Params, param string) int64 { func GetInt(m orm.Params, param string) int64 {
var ret int64 var ret int64
ret = -1 ret = -1
switch i := m[param].(type) { switch i := m[param].(type) {
case int, int32, int64: case int, int32, int64:
ret = i.(int64) ret = i.(int64)
} }
return ret return ret
} }
func GetTime(m orm.Params, param string) time.Time { func GetTime(m orm.Params, param string) time.Time {
var ret time.Time var ret time.Time
switch i := m[param].(type) { switch i := m[param].(type) {
case time.Time: case time.Time:
ret = i ret = i
} }
return ret return ret
} }

View File

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

View File

@ -7,9 +7,10 @@ import (
) )
func init() { func init() {
beego.Router(variables.RootRoute, &controllers.MainController{}) beego.Router(variables.RootRoute, &controllers.MainController{})
beego.Router(variables.AddTempRoute, &controllers.AddTempController{}) beego.Router(variables.AddTempRoute, &controllers.AddTempController{})
beego.Router(variables.ViewTempRoute, &controllers.ViewTempController{}) beego.Router(variables.ViewTempRoute, &controllers.ViewTempController{})
beego.Router(variables.SensorsRoute, &controllers.SensorsController{}) beego.Router(variables.SensorsRoute, &controllers.SensorsController{})
beego.Router(variables.LoginRoute, &controllers.LoginController{}) 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> <script type="text/javascript" src="/static/bootstrap/js/npm.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug --> <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="../../assets/js/ie10-viewport-bug-workaround.js"></script> <script src="../../assets/js/ie10-viewport-bug-workaround.js"></script>
{{template "extrajs" .}}

View File

@ -14,7 +14,17 @@
<div class="form-group"> <div class="form-group">
<label>Interval de prise de mesure : </label> <label>Interval de prise de mesure : </label>
<input type="number" class="form-control" name="interval" value="{{$val.Interval}}"/> <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> </div>
<br/><br/> <br/><br/>
<div class="form-group"> <div class="form-group">

View File

@ -25,7 +25,19 @@
}, { }, {
count: 1, count: 1,
type: 'day', 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, count: 7,
type: 'day', 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}}