Skip to content

Commit 01361af

Browse files
committed
Profile pictures for LDAP users
1 parent 3491f97 commit 01361af

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

lib/letter-avatars.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"use strict";
2+
3+
// external modules
4+
var seedrandom = require('seedrandom');
5+
6+
// core
7+
module.exports = function(name) {
8+
var colors = ["#5A8770", "#B2B7BB", "#6FA9AB", "#F5AF29", "#0088B9", "#F18636", "#D93A37", "#A6B12E", "#5C9BBC", "#F5888D", "#9A89B5", "#407887", "#9A89B5", "#5A8770", "#D33F33", "#A2B01F", "#F0B126", "#0087BF", "#F18636", "#0087BF", "#B2B7BB", "#72ACAE", "#9C8AB4", "#5A8770", "#EEB424", "#407887"];
9+
var color = colors[Math.floor(seedrandom(name)() * colors.length)];
10+
var letter = name.substring(0, 1).toUpperCase();
11+
12+
var svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>';
13+
svg += '<svg xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://www.w3.org/2000/svg" height="96" width="96" version="1.1" viewBox="0 0 96 96">';
14+
svg += '<g>';
15+
svg += '<rect width="96" height="96" fill="' + color + '" />';
16+
svg += '<text font-size="64px" font-family="sans-serif" text-anchor="middle" fill="#ffffff">';
17+
svg += '<tspan x="48" y="72" stroke-width=".26458px" fill="#ffffff">' + letter + '</tspan>';
18+
svg += '</text>';
19+
svg += '</g>';
20+
svg += '</svg>';
21+
22+
return 'data:image/svg+xml;base64,' + new Buffer(svg).toString('base64');
23+
};

lib/models/user.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var scrypt = require('scrypt');
77

88
// core
99
var logger = require("../logger.js");
10+
var letterAvatars = require('../letter-avatars.js');
1011

1112
module.exports = function (sequelize, DataTypes) {
1213
var User = sequelize.define("User", {
@@ -105,6 +106,16 @@ module.exports = function (sequelize, DataTypes) {
105106
case "google":
106107
photo = profile.photos[0].value.replace(/(\?sz=)\d*$/i, '$196');
107108
break;
109+
case "ldap":
110+
//no image api provided,
111+
//use gravatar if email exists,
112+
//otherwise generate a letter avatar
113+
if (profile.emails[0]) {
114+
photo = 'https://www.gravatar.com/avatar/' + md5(profile.emails[0]) + '?s=96';
115+
} else {
116+
photo = letterAvatars(profile.username);
117+
}
118+
break;
108119
}
109120
return photo;
110121
},

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
"reveal.js": "^3.3.0",
100100
"sequelize": "^3.24.3",
101101
"scrypt": "^6.0.3",
102+
"seedrandom": "^2.4.2",
102103
"select2": "^3.5.2-browserify",
103104
"sequelize-cli": "^2.4.0",
104105
"shortid": "2.2.6",

0 commit comments

Comments
 (0)