User adding/editing

This commit is contained in:
Matej Kramny 2015-06-02 16:02:44 +02:00
parent f09478ec58
commit 11bd1e49df
4 changed files with 63 additions and 57 deletions

View File

@ -1,6 +1,6 @@
var models = require('../../models') var models = require('../../models'),
var mongoose = require('mongoose') mongoose = require('mongoose'),
var express = require('express') express = require('express');
exports.unauthorized = function (app, template) { exports.unauthorized = function (app, template) {
template([ template([
@ -27,32 +27,34 @@ exports.router = function (app) {
function get (req, res, next, id) { function get (req, res, next, id) {
models.User.findOne({ models.User.findOne({
_id: id _id: id
}).select('-password').exec(function (err, identity) { })
.select('-password')
.exec(function (err, user) {
if (err || !user) { if (err || !user) {
return res.send(404); return res.send(404);
} }
req.user = user; req._user = user;
next(); next();
}); });
} }
function view (req, res) { function view (req, res) {
res.send(req.user); res.send(req._user);
} }
function save (req, res) { function save (req, res) {
req.user.name = req.body.name; req._user.name = req.body.name;
models.User.hashPassword(req.body.password, function (hash) { models.User.hashPassword(req.body.password, function (hash) {
req.user.password = hash; req._user.password = hash;
}); });
req.user.save(); req._user.save();
res.send(201); res.send(201);
} }
function remove (req, res) { function remove (req, res) {
req.user.remove(function (err) { req._user.remove(function (err) {
res.send(201); res.send(201);
}) })
} }

View File

@ -1,9 +1,7 @@
var models = require('../../models') var models = require('../../models'),
var mongoose = require('mongoose') mongoose = require('mongoose'),
validator = require('validator'),
var user = require('./user') user = require('./user');
var validator = require('validator')
exports.unauthorized = function (app, template) { exports.unauthorized = function (app, template) {
template([ template([
@ -28,25 +26,38 @@ exports.router = function (app) {
} }
function get (req, res) { function get (req, res) {
models.User.find({ models.User.find({})
}).sort('-created').select('-password').exec(function (err, identities) { .sort('-created')
res.send(users) .select('-password')
}) .exec(function (err, users) {
res.send(users);
});
} }
function add (req, res) { function add (req, res) {
if (!validator.isLength(req.body.name, 1)) {
return res.send(400);
}
var user = new models.User({ var user = new models.User({
name: req.body.name, name: req.body.name,
password: req.body.password,
email: req.body.email, email: req.body.email,
username: req.body.username username: req.body.username
}); });
user.save(function () { if (user.name.length == 0 || user.name.email == 0) {
res.send(user); return res.send(400);
}
models.User.findOne({
email: user.email
}, function (_, existingUser) {
if (existingUser) {
return res.send(400);
}
models.User.hashPassword(req.body.password, function (hash) {
user.password = hash;
user.save(function () {
res.send(user);
});
});
}); });
} }

View File

@ -3,30 +3,30 @@ h1 New User
i.fa.fa-fw.fa-chevron-left i.fa.fa-fw.fa-chevron-left
| Back | Back
.row .row
.col-md-6 .col-md-6.col-md-offset-3
.panel.panel-default .panel.panel-default
.panel-body .panel-body
form.form-horizontal(name="userForm") form.form-horizontal(name="userForm")
.form-group .form-group
label.control-label.col-sm-4(for="name") Name label.control-label.col-sm-4(for="name") Name
.col-sm-7 .col-sm-7
input#name.form-control(type="text" placeholder="Display Name" ng-model="user.data.name" autofocus required) input#name.form-control(type="text" placeholder="Display Name" ng-model="user.name" autofocus required)
.form-group
label.control-label.col-sm-4(for="username") Username
.col-sm-7
input#username.form-control(type="text" placeholder="Username" ng-model="user.username" required)
.form-group
label.control-label.col-sm-4(for="email") Email
.col-sm-7
input#email.form-control(type="email" placeholder="Email Address" ng-model="user.email" required)
.form-group .form-group
label.control-label.col-sm-4(for="password") Password label.control-label.col-sm-4(for="password") Password
.col-sm-7 .col-sm-7
input#password.form-control(type="password" placeholder="Vault Password" ng-model="user.data.password" required) input#password.form-control(type="password" placeholder="User Password" ng-model="user.password" required)
.form-group
label.control-label.col-sm-4(for="username") Username
.col-sm-7
input#username.form-control(type="text" placeholder="Private Key" ng-model="user.data.username" required)
.form-group
label.control-label.col-sm-4(for="email") Email
.col-sm-7
textarea#email.form-control(type="text" placeholder="Public Key Contents" ng-model="user.data.email" required)
.form-group .form-group
.col-sm-7.col-sm-offset-4 .col-sm-7.col-sm-offset-4
button.btn.btn-success(ng-click="add()" ng-disabled="userForm.$invalid") Add button.btn.btn-success(ng-click="add()" ng-disabled="userForm.$invalid") Add

View File

@ -1,21 +1,14 @@
define([ define(['app'], function (app) {
'app', app.registerController('AddUserCtrl', ['$scope', '$state', '$http', function($scope, $state, $http) {
'factories/identity', $scope.user = {};
'services/identities'
], function(app) {
app.registerController('AddIdentityCtrl', ['$scope', '$state', 'Identity', function($scope, $state, Identity) {
$scope.identity = new Identity();
$scope.add = function () {
$scope.identity.add()
.success(function (data) {
$state.transitionTo('identity.view', {
identity_id: data._id
});
})
.error(function (data) {
}) $scope.add = function () {
$http.post('/users', $scope.user)
.success(function (data) {
$state.transitionTo('users.list');
}).error(function () {
alert('cannot add user.')
});
} }
}]); }]);
}); });