Added async settings update

Removed /settings GET endpoint (not needed, since AngularJS in use)
TODO: Move the anon struct in use to a model as appropriate
pull/24/head
Jordan 2014-05-28 23:29:41 -05:00
parent e5a3a7875b
commit 7b39096e3f
4 changed files with 40 additions and 24 deletions

View File

@ -1,6 +1,7 @@
package controllers
import (
"encoding/json"
"fmt"
"html/template"
"net/http"
@ -127,29 +128,25 @@ func Base(w http.ResponseWriter, r *http.Request) {
}
func Settings(w http.ResponseWriter, r *http.Request) {
params := struct {
User models.User
Title string
Flashes []interface{}
Token string
}{Title: "Settings", User: ctx.Get(r, "user").(models.User)}
session := ctx.Get(r, "session").(*sessions.Session)
switch {
case r.Method == "GET":
params.Token = nosurf.Token(r)
params.Flashes = session.Flashes()
session.Save(r, w)
getTemplate(w, "settings").ExecuteTemplate(w, "base", params)
case r.Method == "POST":
err := auth.ChangePassword(r)
msg := struct {
Message string `json:"message"`
Success bool `json:"success"`
}{Message: "Settings Updated Successfully", Success: true}
if err == auth.ErrInvalidPassword {
Flash(w, r, "danger", "Invalid Password")
msg.Message = "Invalid Password"
msg.Success = false
} else if err != nil {
Flash(w, r, "danger", "Unknown Error")
} else {
Flash(w, r, "success", "Password successfully reset")
msg.Message = "Unknown Error Occured"
msg.Success = false
}
http.Redirect(w, r, "/settings", 302)
msgj, err := json.MarshalIndent(msg, "", " ")
if checkError(err, w, "Error marshaling response", http.StatusInternalServerError) {
return
}
writeJSON(w, msgj)
}
}

View File

@ -356,6 +356,7 @@ app.controller('SettingsCtrl', function($scope, $http, $window) {
})
}
$scope.form_data = {
username: user.username,
csrf_token: csrf_token
}
$scope.api_reset = function() {
@ -373,4 +374,22 @@ app.controller('SettingsCtrl', function($scope, $http, $window) {
$scope.successFlash("API Key Successfully Reset")
})
}
$scope.save_settings = function(){
$http({
method: 'POST',
url: '/settings',
data: $.param($scope.form_data),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.success(function(data) {
if (data.success) {
$scope.successFlash(data.message)
}
else {
$scope.errorFlash(data.message)
}
})
}
})

View File

@ -32,32 +32,32 @@
</div>
<form ng-submit="api_reset()">
<button class="btn btn-primary"><i class="fa fa-refresh" type="submit"></i> Reset</button>
<input type="hidden" name="csrf_token" value="{{form.csrf_token}}"/>
<input type="hidden" name="csrf_token" value="{{form_data.csrf_token}}"/>
</form>
</div>
<br />
<form action="/settings" method="POST">
<form ng-submit="save_settings()" method="POST">
<div class="row">
<label for="username" class="col-sm-2 control-label form-label">Username:</label>
<div class="col-md-6">
<input type="text" id="username" value="{{user.username}}" class="form-control" />
<input type="text" id="username" class="form-control" ng-model="form_data.username" />
</div>
</div>
<br />
<div class="row">
<label for="current_password" class="col-sm-2 control-label form-label">Old Password:</label>
<div class="col-md-6">
<input type="password" id="current_password" name="current_password" class="form-control" />
<input type="password" id="current_password" name="current_password" class="form-control" ng-model="form_data.current_password" />
</div>
</div>
<br />
<div class="row">
<label for="new_password" class="col-sm-2 control-label form-label">New Password:</label>
<div class="col-md-6">
<input type="password" id="new_password" name="new_password" class="form-control" />
<input type="password" id="new_password" name="new_password" class="form-control" ng-model="form_data.new_password" />
</div>
</div>
<input type="hidden" name="csrf_token" value=/>
<input type="hidden" name="csrf_token" ng-model="form_data.csrf_token"/>
<br />
<button class="btn btn-primary" type="submit">Save</button>
</form>

View File

@ -44,7 +44,7 @@
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu" style="left:auto; right:0;" role="menu">
<li><a ng-click="editGroup(group)" data-toggle="modal" ng-href="#" data-target="#newGroupModal">Edit</a>
<li><a ng-click="editGroup(group)">Edit</a>
</li>
<li class="divider"></li>
<li><a ng-click="deleteGroup(group)" ng-href="#">Delete</a>