mirror of
https://github.com/wekan/wekan.git
synced 2025-09-22 01:50:48 +02:00
Improve Sandstorm usernames management
We now use the `preferredHandle` exposed by Sandstorm as source for the username and append a number if the username is already taken since we need to ensure username uniqueness (eg 'max', 'max1', 'max2') Fixes #352
This commit is contained in:
parent
fc82f7227a
commit
cb3bd86396
3 changed files with 30 additions and 7 deletions
|
@ -64,7 +64,7 @@ jquery@1.11.4
|
||||||
kadira:blaze-layout@2.2.0
|
kadira:blaze-layout@2.2.0
|
||||||
kadira:dochead@1.3.2
|
kadira:dochead@1.3.2
|
||||||
kadira:flow-router@2.8.0
|
kadira:flow-router@2.8.0
|
||||||
kenton:accounts-sandstorm@0.1.7
|
kenton:accounts-sandstorm@0.1.8
|
||||||
launch-screen@1.0.4
|
launch-screen@1.0.4
|
||||||
livedata@1.0.15
|
livedata@1.0.15
|
||||||
localstorage@1.0.5
|
localstorage@1.0.5
|
||||||
|
|
|
@ -89,7 +89,7 @@ template(name="addMemberPopup")
|
||||||
a.name.js-select-member(title="{{profile.name}} ({{username}})")
|
a.name.js-select-member(title="{{profile.name}} ({{username}})")
|
||||||
+userAvatar(userId=_id esSearch=true)
|
+userAvatar(userId=_id esSearch=true)
|
||||||
span.full-name
|
span.full-name
|
||||||
= profile.name
|
= profile.fullname
|
||||||
| (<span class="username">{{username}}</span>)
|
| (<span class="username">{{username}}</span>)
|
||||||
if isBoardMember
|
if isBoardMember
|
||||||
.quiet ({{_ 'joined'}})
|
.quiet ({{_ 'joined'}})
|
||||||
|
|
33
sandstorm.js
33
sandstorm.js
|
@ -22,8 +22,8 @@ if (isSandstorm && Meteor.isServer) {
|
||||||
};
|
};
|
||||||
|
|
||||||
function updateUserPermissions(userId, permissions) {
|
function updateUserPermissions(userId, permissions) {
|
||||||
const isActive = permissions.includes('participate');
|
const isActive = permissions.indexOf('participate') > -1;
|
||||||
const isAdmin = permissions.includes('configure');
|
const isAdmin = permissions.indexOf('configure') > -1;
|
||||||
const permissionDoc = { userId, isActive, isAdmin };
|
const permissionDoc = { userId, isActive, isAdmin };
|
||||||
|
|
||||||
const boardMembers = Boards.findOne(sandstormBoard._id).members;
|
const boardMembers = Boards.findOne(sandstormBoard._id).members;
|
||||||
|
@ -78,17 +78,40 @@ if (isSandstorm && Meteor.isServer) {
|
||||||
// unique board document. Note that when the `Users.after.insert` hook is
|
// unique board document. Note that when the `Users.after.insert` hook is
|
||||||
// called, the user is inserted into the database but not connected. So
|
// called, the user is inserted into the database but not connected. So
|
||||||
// despite the appearances `userId` is null in this block.
|
// despite the appearances `userId` is null in this block.
|
||||||
//
|
|
||||||
// XXX We should support the `preferredHandle` exposed by Sandstorm
|
|
||||||
Users.after.insert((userId, doc) => {
|
Users.after.insert((userId, doc) => {
|
||||||
if (!Boards.findOne(sandstormBoard._id)) {
|
if (!Boards.findOne(sandstormBoard._id)) {
|
||||||
Boards.insert(sandstormBoard, {validate: false});
|
Boards.insert(sandstormBoard, { validate: false });
|
||||||
Activities.update(
|
Activities.update(
|
||||||
{ activityTypeId: sandstormBoard._id },
|
{ activityTypeId: sandstormBoard._id },
|
||||||
{ $set: { userId: doc._id }}
|
{ $set: { userId: doc._id }}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We rely on username uniqueness for the user mention feature, but
|
||||||
|
// Sandstorm doesn't enforce this property -- see #352. Our strategy to
|
||||||
|
// generate unique usernames from the Sandstorm `preferredHandle` is to
|
||||||
|
// append a number that we increment until we generate a username that no
|
||||||
|
// one already uses (eg, 'max', 'max1', 'max2').
|
||||||
|
function generateUniqueUsername(username, appendNumber) {
|
||||||
|
return username + String(appendNumber === 0 ? '' : appendNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
const username = doc.services.sandstorm.preferredHandle;
|
||||||
|
let appendNumber = 0;
|
||||||
|
while (Users.findOne({
|
||||||
|
_id: { $ne: doc._id },
|
||||||
|
username: generateUniqueUsername(username, appendNumber),
|
||||||
|
})) {
|
||||||
|
appendNumber += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Users.update(doc._id, {
|
||||||
|
$set: {
|
||||||
|
username: generateUniqueUsername(username, appendNumber),
|
||||||
|
'profile.fullname': doc.services.sandstorm.name,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
updateUserPermissions(doc._id, doc.services.sandstorm.permissions);
|
updateUserPermissions(doc._id, doc.services.sandstorm.permissions);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue