Skip to content

Commit

Permalink
Merge pull request #252 from feup-infolab/avatarWorkingVersion
Browse files Browse the repository at this point in the history
Avatar working version
  • Loading branch information
silvae86 committed Jul 11, 2017
2 parents 13416dd + 32c0561 commit d5dfaa6
Show file tree
Hide file tree
Showing 30 changed files with 1,682 additions and 258 deletions.
2 changes: 1 addition & 1 deletion conf/deployment_configs.json
Expand Up @@ -174,7 +174,7 @@
"load_databases" : true,
"reload_administrators_on_startup" : true,
"reload_demo_users_on_startup" : true,
"reload_ontologies_on_startup": true
"reload_ontologies_on_startup": false
},
"baselines" : {

Expand Down
4 changes: 3 additions & 1 deletion public/app/js/app.js
Expand Up @@ -32,7 +32,9 @@ var dendroApp = angular.module('dendroApp', [
'dendroApp.services',
'dendroApp.directives',
'dendroApp.factories',
'imageSpinner'
"imageSpinner",
"ngImgCrop",
"ngImageCompress"
]).filter('trustAsResourceUrl', ['$sce', function($sce) {
return function(val) {
return $sce.trustAsResourceUrl(val);
Expand Down
44 changes: 44 additions & 0 deletions public/app/js/controllers/avatar/avatar_controller.js
@@ -0,0 +1,44 @@
angular.module("dendroApp.controllers")
/**
* Avatar controller
*/
.controller("avatarCtrl", function ($scope, $http, $filter, $window, $element, usersService)
{
$scope.editAvatarModalActivated = false;
$scope.myImage="";
$scope.myCroppedImage="";
$scope.imageCompressed = null;
$scope.avatarUri = "";

$scope.openEditAvatarModal = function () {
$scope.editAvatarModalActivated = true;
};

$scope.closeEditAvatarModal = function () {
$scope.editAvatarModalActivated = false;
$scope.myCroppedImage = null;
};

$scope.updateProfilePic = function () {
usersService.updateAvatar($scope.myCroppedImage)
.then(function(response)
{
location.reload();
})
.catch(function(error){
console.error("Error updating avatar " + JSON.stringify(error));
});
};

var handleFileSelect=function(compressedImage) {
$scope.myImage=compressedImage.compressed.dataURL;
};

$scope.$watch("imageCompressed", function() {
if($scope.imageCompressed)
{
handleFileSelect($scope.imageCompressed);
}
});

});
31 changes: 31 additions & 0 deletions public/app/js/services/users_service.js
Expand Up @@ -62,4 +62,35 @@ angular.module('dendroApp.services')
});
}
};

this.updateAvatar = function(newAvatarPicture)
{
var requestUri = "/user/avatar";

var params = {
newAvatar : newAvatarPicture
};

return $http({
method: "POST",
url: requestUri,
data: params,
contentType: "application/json",
headers: {'Accept': "application/json"}
//contentType: "data:image/png;base64",
//headers: {'Accept': "data:image/png;base64"}
});
};

this.receiveAvatar = function(username)
{
var requestUri = "/user/" + username +"/avatar";

return $http({
method: "GET",
url: requestUri,
contentType: "application/json",
headers: {'Accept': "application/json"}
});
};
}]);
4 changes: 3 additions & 1 deletion public/bower.json
Expand Up @@ -73,7 +73,9 @@
"bootswatch": "https://github.com/thomaspark/bootswatch.git",
"angular-image-spinner": "*",
"spin.js": "~2.0.0",
"parsleyjs": "^2.7.2"
"parsleyjs": "^2.7.2",
"ng-img-crop": "^0.3.2",
"angular-image-compress": "^0.1.5"
},
"resolutions": {
"bootstrap": "~3",
Expand Down
Binary file added public/images/default_avatar/defaultAvatar.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions public/stylesheets/explorer.css
Expand Up @@ -180,3 +180,32 @@ Animations (uncomment to have animations)
.file_explorer_icon {
text-shadow: 1px 1px 1px #ccc;
}
.social-dendro-btn {
font-size: 10px;
}
.cropArea {
background: #e4e4e4;
overflow: hidden;
width: 500px;
height: 350px;
}
.show-avatar-in-profile {
width: 5%;
height: 5%;
}
.show-avatar-in-header {
width: 28px;
height: 28px;
position: relative;
top: -5px;
float: left;
left: -5px;
}
.show-avatar-in-create-post {
width: 50%;
height: 50%;
}
.show-author-avatar-partial {
width: 50%;
height: 50%;
}
35 changes: 35 additions & 0 deletions public/stylesheets/explorer.styl
Expand Up @@ -194,6 +194,41 @@ Animations (uncomment to have animations)
text-shadow: 1px 1px 1px #ccc;
}

.social-dendro-btn
{
font-size: 10px;
}

.cropArea {
background: #E4E4E4;
overflow: hidden;
width:500px;
height:350px;
}

.show-avatar-in-profile
{
width: 5%;
height: 5%;
}

.show-avatar-in-header {
width: 28px;
height: 28px;
position: relative;
top: -5px;
float: left;
left: -5px;
}

.show-avatar-in-create-post {
width: 50%;
height: 50%;
}

.show-author-avatar-partial {
width: 50%;
height: 50%;
}


67 changes: 38 additions & 29 deletions src/app.js
Expand Up @@ -1226,40 +1226,46 @@ async.series([
function(username, password, done) {
const User = require(Config.absPathInSrcFolder("/models/user.js")).User;
User.findByUsername(username, function (err, user) {

const bcrypt = require('bcryptjs');
bcrypt.hash(password, user.ddr.salt, function(err, hashedPassword) {
if(!err) {
if (!isNull(user)) {
if (user.ddr.password === hashedPassword) {
user.isAdmin(function (err, isAdmin) {
if (!err) {
return done(
err,
user,
{
isAdmin : isAdmin
});
}
else {
return done("Unable to check for admin user when authenticating with username " + username, null);
}
});
if(!err && !isNull(user))
{
const bcrypt = require('bcryptjs');
bcrypt.hash(password, user.ddr.salt, function(err, hashedPassword) {
if(!err) {
if (!isNull(user)) {
if (user.ddr.password === hashedPassword) {
user.isAdmin(function (err, isAdmin) {
if (!err) {
return done(
err,
user,
{
isAdmin : isAdmin
});
}
else {
return done("Unable to check for admin user when authenticating with username " + username, false);
}
});
}
else {
return done("Invalid username/password combination.", false);
}
}
else {
return done("Invalid username/password combination.", null);
console.error(err.stack);
return done("Unknown error during authentication, calculating password hash.", false);
}
}
else {
console.error(err.stack);
return done("Unknown error during authentication, calculating password hash.", null);
else
{
return done("There is no user with username " + username + " registered in this system.", false);
}
}
else
{
return done("There is no user with username " + username + " registered in this system.", null);
}
});
});
}
else
{
return done("Invalid username/password combination.", false);
}
});
}
));
Expand Down Expand Up @@ -1337,6 +1343,9 @@ async.series([
app.get('/user/:username', async.apply(Permissions.require, [Permissions.role.system.user]), users.show);
app.get('/username_exists', users.username_exists);
app.get('/users/loggedUser', users.getLoggedUser);
app.get('/user/:username/avatar', async.apply(Permissions.require, [Permissions.role.system.user]), users.get_avatar);
app.post('/user/avatar', async.apply(Permissions.require, [Permissions.role.system.user]), users.upload_avatar);
app.post('/user/edit', async.apply(Permissions.require, [Permissions.role.system.user]), users.edit);

app.all('/reset_password', users.reset_password);
app.all('/set_new_password', users.set_new_password);
Expand Down
37 changes: 22 additions & 15 deletions src/controllers/auth.js
Expand Up @@ -26,7 +26,7 @@ module.exports.login = function(req, res, next){
req.passport.authenticate(
'local',
{
successRedirect: '/user/me',
//successRedirect: '/projects/my',
failureRedirect: '/login',
failureFlash: true
},
Expand Down Expand Up @@ -79,12 +79,20 @@ module.exports.login = function(req, res, next){
}
else
{
res.status(401).json(
{
result : "error",
message : err
}
);
if(acceptsJSON && !acceptsHTML) //will be null if the client does not accept html
{
res.status(401).json(
{
result : "error",
message : err
}
);
}
else
{
req.flash("error", err);
res.redirect("/login");
}
}
}
)(req, res, next);
Expand Down Expand Up @@ -204,7 +212,7 @@ module.exports.register = function(req, res){
}
);
}
else if(!isNull(req.body.username) && !req.body.username.mat0ch(/^[0-9a-zA-Z]+$/))
else if(!isNull(req.body.username) && !req.body.username.match(/^[0-9a-zA-Z]+$/))
{
res.render('auth/register',
{
Expand All @@ -226,7 +234,7 @@ module.exports.register = function(req, res){
}
else
{
if(typeof req.body.password === req.body.repeat_password)
if(req.body.password === req.body.repeat_password)
{
const userData = {
ddr : {
Expand Down Expand Up @@ -338,16 +346,15 @@ module.exports.register = function(req, res){
], function(err, user){
if(!err)
{
res.render('/login', {
success_messages : [user]
});
req.flash("success", user);
res.redirect("/login");
}
else
{

req.flash("error", "Error registering a new user");
console.error("Error registering a new user: " + JSON.stringify(err));
res.redirect("/register");
}


});
}
}
Expand Down

0 comments on commit d5dfaa6

Please sign in to comment.