From 11bd1e49dfa1b79fc81b9a6f109e207d72f4c624 Mon Sep 17 00:00:00 2001 From: Matej Kramny Date: Tue, 2 Jun 2015 16:02:44 +0200 Subject: [PATCH] User adding/editing --- lib/routes/user/user.js | 22 ++++++++------- lib/routes/user/users.js | 47 +++++++++++++++++++------------ lib/views/user/add.jade | 24 ++++++++-------- public/js/controllers/user/add.js | 27 +++++++----------- 4 files changed, 63 insertions(+), 57 deletions(-) diff --git a/lib/routes/user/user.js b/lib/routes/user/user.js index 7b276d71..ea970214 100644 --- a/lib/routes/user/user.js +++ b/lib/routes/user/user.js @@ -1,6 +1,6 @@ -var models = require('../../models') -var mongoose = require('mongoose') -var express = require('express') +var models = require('../../models'), + mongoose = require('mongoose'), + express = require('express'); exports.unauthorized = function (app, template) { template([ @@ -27,32 +27,34 @@ exports.router = function (app) { function get (req, res, next, id) { models.User.findOne({ _id: id - }).select('-password').exec(function (err, identity) { + }) + .select('-password') + .exec(function (err, user) { if (err || !user) { return res.send(404); } - req.user = user; + req._user = user; next(); }); } function view (req, res) { - res.send(req.user); + res.send(req._user); } function save (req, res) { - req.user.name = req.body.name; + req._user.name = req.body.name; 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); } function remove (req, res) { - req.user.remove(function (err) { + req._user.remove(function (err) { res.send(201); }) } diff --git a/lib/routes/user/users.js b/lib/routes/user/users.js index 4253bcec..39a0579d 100644 --- a/lib/routes/user/users.js +++ b/lib/routes/user/users.js @@ -1,9 +1,7 @@ -var models = require('../../models') -var mongoose = require('mongoose') - -var user = require('./user') - -var validator = require('validator') +var models = require('../../models'), + mongoose = require('mongoose'), + validator = require('validator'), + user = require('./user'); exports.unauthorized = function (app, template) { template([ @@ -28,25 +26,38 @@ exports.router = function (app) { } function get (req, res) { - models.User.find({ - }).sort('-created').select('-password').exec(function (err, identities) { - res.send(users) - }) + models.User.find({}) + .sort('-created') + .select('-password') + .exec(function (err, users) { + res.send(users); + }); } function add (req, res) { - if (!validator.isLength(req.body.name, 1)) { - return res.send(400); - } - var user = new models.User({ name: req.body.name, - password: req.body.password, email: req.body.email, username: req.body.username }); - user.save(function () { - res.send(user); + if (user.name.length == 0 || user.name.email == 0) { + 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); + }); + }); }); -} +} \ No newline at end of file diff --git a/lib/views/user/add.jade b/lib/views/user/add.jade index 515905c1..7a5e88c1 100644 --- a/lib/views/user/add.jade +++ b/lib/views/user/add.jade @@ -3,30 +3,30 @@ h1 New User i.fa.fa-fw.fa-chevron-left | Back .row - .col-md-6 + .col-md-6.col-md-offset-3 .panel.panel-default .panel-body form.form-horizontal(name="userForm") .form-group label.control-label.col-sm-4(for="name") Name .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 label.control-label.col-sm-4(for="password") Password .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 .col-sm-7.col-sm-offset-4 button.btn.btn-success(ng-click="add()" ng-disabled="userForm.$invalid") Add diff --git a/public/js/controllers/user/add.js b/public/js/controllers/user/add.js index 7b0d0037..a5235bcd 100644 --- a/public/js/controllers/user/add.js +++ b/public/js/controllers/user/add.js @@ -1,21 +1,14 @@ -define([ - 'app', - 'factories/identity', - '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) { +define(['app'], function (app) { + app.registerController('AddUserCtrl', ['$scope', '$state', '$http', function($scope, $state, $http) { + $scope.user = {}; - }) + $scope.add = function () { + $http.post('/users', $scope.user) + .success(function (data) { + $state.transitionTo('users.list'); + }).error(function () { + alert('cannot add user.') + }); } }]); }); \ No newline at end of file