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"
)

View File

@ -15,7 +15,6 @@ var (
_, timezoneOffset = time.Now().Zone()
)
type SensorPrint struct {
Name string
Mac string
@ -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

@ -17,7 +17,7 @@ func (c *LoginController) Prepare() {
func (c *LoginController) Get() {
sess := c.GetSession(variables.SessionName)
if sess != nil {
c.Redirect(variables.RootRoute, 302)
c.Redirect(variables.UserRoute, 302)
return
}
@ -39,13 +39,10 @@ func (c *LoginController) Post() {
}
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)
return ret && pwd == usr.Password

View File

@ -7,8 +7,8 @@ import (
"datahouse/models/temperature"
"datahouse/models/variables"
"strconv"
"fmt"
"strconv"
)
type SensorsController struct {
@ -25,7 +25,6 @@ func (c *SensorsController) Prepare() {
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

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,7 +20,7 @@ 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)

View File

@ -7,7 +7,6 @@ import (
"datahouse/models/utils"
)
type SensorTable struct {
Id int64
SensorMAC string
@ -19,8 +18,7 @@ func init() {
orm.RegisterModel(new(SensorTable))
}
func GetSensorByMac(sensorMac string) (*SensorTable) {
func GetSensorByMac(sensorMac string) *SensorTable {
o := orm.NewOrm()
o.Using(database.Alias)
@ -41,7 +39,7 @@ func GetSensorByMac(sensorMac string) (*SensorTable) {
return ret
}
func GetAllSensorIds() ([]int64) {
func GetAllSensorIds() []int64 {
o := orm.NewOrm()
o.Using(database.Alias)
@ -57,7 +55,7 @@ func GetAllSensorIds() ([]int64) {
return ret
}
func GetAllSensor() ([]SensorTable) {
func GetAllSensor() []SensorTable {
o := orm.NewOrm()
o.Using(database.Alias)
@ -79,7 +77,7 @@ func GetAllSensor() ([]SensorTable) {
return ret
}
func GetSensor(id int64) (*SensorTable) {
func GetSensor(id int64) *SensorTable {
o := orm.NewOrm()
o.Using(database.Alias)
@ -98,7 +96,6 @@ func GetSensor(id int64) (*SensorTable) {
return ret
}
func UpdateSensor(mac, description string, interval int64) {
o := orm.NewOrm()
o.Using(database.Alias)
@ -111,7 +108,6 @@ func UpdateSensor(mac, description string, interval int64) {
}
}
func AddSensor(sensorMac string) {
o := orm.NewOrm()
o.Using(database.Alias)

View File

@ -19,13 +19,10 @@ type TempTableTmp struct {
Value int64
}
func init() {
orm.RegisterModel(new(TempTableTmp))
}
/**
* Add the value into the database.
*/
@ -39,8 +36,7 @@ func AddData(sensor, value int64) {
o.Insert(tmpTable)
}
func GetTemp(sensorId int64) (*TempTableTmp) {
func GetTemp(sensorId int64) *TempTableTmp {
o := orm.NewOrm()
o.Using(database.Alias)
@ -58,7 +54,6 @@ func GetTemp(sensorId int64) (*TempTableTmp) {
return data
}
func UpdateTemp(sensorId, val int64, date time.Time) {
o := orm.NewOrm()
o.Using(database.Alias)

View File

@ -20,7 +20,6 @@ func init() {
orm.RegisterModel(new(TempTable))
}
/**
* Add the value into the database.
*/
@ -34,7 +33,7 @@ func AddData(sensor, value int64) {
o.Insert(tmpTable)
}
func GetAllTempForSensor(sensorId int64) ([]TempTable) {
func GetAllTempForSensor(sensorId int64) []TempTable {
o := orm.NewOrm()
o.Using(database.Alias)
@ -55,7 +54,7 @@ func GetAllTempForSensor(sensorId int64) ([]TempTable) {
return dataArray
}
func GetLastTempForSensor(sensorId int64) (*TempTable) {
func GetLastTempForSensor(sensorId int64) *TempTable {
o := orm.NewOrm()
o.Using(database.Alias)

View File

@ -8,21 +8,18 @@ import (
"datahouse/models/utils"
)
type User struct {
Id int64
Login string
Password string
}
func init() {
// register model
orm.RegisterModel(new(User))
}
func IsUserExist(login string) (bool) {
func IsUserExist(login string) bool {
o := orm.NewOrm()
o.Using(database.Alias)
@ -38,7 +35,7 @@ func IsUserExist(login string) (bool) {
return ret
}
func GetUserByLogin(login string) (*User) {
func GetUserByLogin(login string) *User {
o := orm.NewOrm()
o.Using(database.Alias)
@ -56,8 +53,7 @@ func GetUserByLogin(login string) (*User) {
return ret
}
func GetUser(id int64) (*User) {
func GetUser(id int64) *User {
o := orm.NewOrm()
o.Using(database.Alias)
@ -75,10 +71,20 @@ func GetUser(id int64) (*User) {
return ret
}
func AddUser(login, sha256Pass string) {
o := orm.NewOrm()
o.Using(database.Alias)
_, _ = 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

@ -6,7 +6,6 @@ import (
"time"
)
func GetString(m orm.Params, param string) string {
ret := ""
switch i := m[param].(type) {

View File

@ -12,4 +12,5 @@ var (
ViewTempRoute = "/view/temp"
SensorsRoute = "/sensors"
LoginRoute = "/login"
UserRoute = "/user"
)

View File

@ -12,4 +12,5 @@ func init() {
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}}