|
|
@@ -478,29 +478,39 @@ module.exports = function(crowi, app) {
|
|
|
actions.userGroup = {};
|
|
|
actions.userGroup.index = function (req, res) {
|
|
|
var page = parseInt(req.query.page) || 1;
|
|
|
+ var renderVar = {
|
|
|
+ userGroups : [],
|
|
|
+ userGroupRelations : new Map(),
|
|
|
+ pager : null,
|
|
|
+ }
|
|
|
|
|
|
- UserGroup.findUserGroupsWithPagination({ page: page }, function (err, result) {
|
|
|
- const pager = createPager(result.total, result.limit, result.page, result.pages, MAX_PAGE_LIST);
|
|
|
- var userGroups = result.docs
|
|
|
- var groupRelations = new Map();
|
|
|
- UserGroupRelation.findAllRelationForUserGroups(userGroups)
|
|
|
- .then( function(relations) {
|
|
|
- userGroups.map((group) => {
|
|
|
- groupRelations.set(group, relations.filter( function(target) {
|
|
|
- return target.relatedGroup.toString() == group._id.toString();
|
|
|
- }));
|
|
|
- });
|
|
|
- return res.render('admin/user-groups', {
|
|
|
- userGroups: userGroups,
|
|
|
- userGroupRelations: groupRelations,
|
|
|
- pager: pager
|
|
|
+ UserGroup.findUserGroupsWithPagination({ page: page })
|
|
|
+ .then((result) => {
|
|
|
+ const pager = createPager(result.total, result.limit, result.page, result.pages, MAX_PAGE_LIST);
|
|
|
+ var userGroups = result.docs;
|
|
|
+ renderVar.userGroups = userGroups;
|
|
|
+ renderVar.pager = pager;
|
|
|
+ return userGroups.map((userGroup) => {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ UserGroupRelation.findAllRelationForUserGroup(userGroup)
|
|
|
+ .then((relations) => {
|
|
|
+ return resolve([userGroup, relations]);
|
|
|
+ });
|
|
|
});
|
|
|
- })
|
|
|
- .catch( function(err) {
|
|
|
- debug('Error on find all relations', err);
|
|
|
- return res.json(ApiResponse.error('Error'));
|
|
|
});
|
|
|
- });
|
|
|
+ })
|
|
|
+ .then((allRelationsPromise) => {
|
|
|
+ return Promise.all(allRelationsPromise)
|
|
|
+ })
|
|
|
+ .then((relations) => {
|
|
|
+ renderVar.userGroupRelations = new Map(relations);
|
|
|
+ debug("in findUserGroupsWithPagination findAllRelationForUserGroupResult", renderVar.userGroupRelations);
|
|
|
+ return res.render('admin/user-groups', renderVar);
|
|
|
+ })
|
|
|
+ .catch( function(err) {
|
|
|
+ debug('Error on find all relations', err);
|
|
|
+ return res.json(ApiResponse.error('Error'));
|
|
|
+ });
|
|
|
};
|
|
|
|
|
|
// グループ詳細
|
|
|
@@ -512,56 +522,39 @@ module.exports = function(crowi, app) {
|
|
|
pageGroupRelations: [],
|
|
|
notRelatedusers: []
|
|
|
}
|
|
|
+ var targetUserGroup = null;
|
|
|
UserGroup.findUserGroupByName(name)
|
|
|
.then(function (userGroup) {
|
|
|
- if (userGroup == null) {
|
|
|
- req.flash('errorMessage', 'ユーザグループの検索に失敗しました');
|
|
|
- debug('Error on get userGroupDetail', err);
|
|
|
- return res.redirect('/admin/user-groups');
|
|
|
- } else {
|
|
|
- renderVar.userGroup = userGroup;
|
|
|
-
|
|
|
- UserGroupRelation.findAllRelationForUserGroup(userGroup)
|
|
|
- .then(function (relations) {
|
|
|
- renderVar.userGroupRelations = relations;
|
|
|
-
|
|
|
- PageGroupRelation.findAllRelationForUserGroup(userGroup)
|
|
|
- .then(function (pageRelations) {
|
|
|
- renderVar.pageGroupRelations = pageRelations;
|
|
|
-
|
|
|
- User.findAllUsers(null)
|
|
|
- .then(function (users) {
|
|
|
- debug('users', users);
|
|
|
- users = users.filter( function(user) {
|
|
|
- var relation = relations.find( function(relation) {
|
|
|
- return relation.relatedUser._id.toString() == user._id.toString();
|
|
|
- });
|
|
|
- return relation == null;
|
|
|
- });
|
|
|
- debug('users', users);
|
|
|
- debug('user-group-detail succeed', relations);
|
|
|
- renderVar.notRelatedusers = users;
|
|
|
- return res.render('admin/user-group-detail', renderVar);
|
|
|
- })
|
|
|
- .catch(function(err) {
|
|
|
- req.flash('errorMessage', 'ユーザの検索に失敗しました');
|
|
|
- debug('Error on find all unrelated users', err);
|
|
|
- return res.redirect('/admin/user-groups');
|
|
|
- });
|
|
|
- })
|
|
|
- .catch(function (err) {
|
|
|
- req.flash('errorMessage', 'グループ関連ページの検索に失敗しました');
|
|
|
- debug('Error on find all relations', err);
|
|
|
- return res.redirect('/admin/user-groups');
|
|
|
- });
|
|
|
- }).catch(function (err) {
|
|
|
- req.flash('errorMessage', 'グループ関連ユーザの検索に失敗しました');
|
|
|
- debug('Error on find all relations', err);
|
|
|
- return res.redirect('/admin/user-groups');
|
|
|
- });
|
|
|
+ targetUserGroup = userGroup;
|
|
|
+ if (targetUserGroup == null) {
|
|
|
+ req.flash('errorMessage', 'グループがありません');
|
|
|
+ return new Promise();
|
|
|
}
|
|
|
+ else {
|
|
|
+ renderVar.userGroup = targetUserGroup;
|
|
|
+
|
|
|
+ // get all user and group relations
|
|
|
+ return UserGroupRelation.findAllRelationForUserGroup(targetUserGroup);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .then((relations) => {
|
|
|
+ renderVar.userGroupRelations = relations;
|
|
|
+
|
|
|
+ // get all page and group relations
|
|
|
+ return PageGroupRelation.findAllRelationForUserGroup(targetUserGroup)
|
|
|
})
|
|
|
- .catch(function(err) {
|
|
|
+ .then(function (pageRelations) {
|
|
|
+ renderVar.pageGroupRelations = pageRelations;
|
|
|
+
|
|
|
+ // get all not related users for group
|
|
|
+ return UserGroupRelation.findUserByNotRelatedGroup(targetUserGroup);
|
|
|
+ })
|
|
|
+ .then((notRelatedusers) => {
|
|
|
+ renderVar.notRelatedusers = notRelatedusers;
|
|
|
+
|
|
|
+ return res.render('admin/user-group-detail', renderVar);
|
|
|
+ })
|
|
|
+ .catch((err) => {
|
|
|
req.flash('errorMessage', 'ユーザグループの検索に失敗しました');
|
|
|
debug('Error on get userGroupDetail', err);
|
|
|
return res.redirect('/admin/user-groups');
|
|
|
@@ -572,23 +565,22 @@ module.exports = function(crowi, app) {
|
|
|
actions.userGroup.create = function (req, res) {
|
|
|
var form = req.form.createGroupForm;
|
|
|
if (req.form.isValid) {
|
|
|
- UserGroup.isRegisterableName(form.userGroupName, function (registerable){
|
|
|
+ UserGroup.isRegisterableName(form.userGroupName)
|
|
|
+ .then((registerable) => {
|
|
|
if (registerable) {
|
|
|
- UserGroup.createGroupByName(form.userGroupName, function (err, newUserGroup) {
|
|
|
- if (err) {
|
|
|
- req.flash('errorMessage', req.form.errors.join('\n'));
|
|
|
- } else {
|
|
|
- req.flash('successMessage', newUserGroup.name)
|
|
|
- req.flash('createdUserGroup', newUserGroup);
|
|
|
- }
|
|
|
- return res.redirect('/admin/user-groups');
|
|
|
+ return UserGroup.createGroupByName(form.userGroupName)
|
|
|
+ .then((newUserGroup) => {
|
|
|
+ req.flash('successMessage', newUserGroup.name)
|
|
|
+ req.flash('createdUserGroup', newUserGroup);
|
|
|
});
|
|
|
}
|
|
|
else {
|
|
|
- req.flash('errorMessage', '同じグループ名が既に存在します。');
|
|
|
debug('userGroupName', form.userGroupName);
|
|
|
- return res.redirect('/admin/user-groups');
|
|
|
+ req.flash('errorMessage', '同じグループ名が既に存在します。');
|
|
|
}
|
|
|
+ })
|
|
|
+ .then(() => {
|
|
|
+ return res.redirect('/admin/user-groups');
|
|
|
});
|
|
|
} else {
|
|
|
req.flash('errorMessage', req.form.errors.join('\n'));
|
|
|
@@ -602,31 +594,34 @@ module.exports = function(crowi, app) {
|
|
|
var userGroupId = req.params.userGroupId;
|
|
|
var name = req.body.name;
|
|
|
|
|
|
- UserGroup.findById(userGroupId, function (err, userGroupData) {
|
|
|
- if (!userGroupData) {
|
|
|
+ UserGroup.findById(userGroupId)
|
|
|
+ .then((userGroupData) => {
|
|
|
+ if (userGroupData == null) {
|
|
|
req.flash('errorMessage', 'グループの検索に失敗しました。');
|
|
|
- return res.redirect('/admin/user-groups');
|
|
|
+ return new Promise();
|
|
|
}
|
|
|
-
|
|
|
- // 名前存在チェック
|
|
|
- UserGroup.isRegisterableName(name, function (isRegisterableName) {
|
|
|
- // 既に存在するグループ名に更新しようとした場合はエラー
|
|
|
- if (!isRegisterableName) {
|
|
|
- req.flash('errorMessage', 'グループ名が既に存在します。');
|
|
|
- return res.redirect('/admin/user-group-detail/' + userGroupData.name);
|
|
|
- }
|
|
|
-
|
|
|
- userGroupData.updateName(name, function (err, updatedName) {
|
|
|
- if (err) {
|
|
|
- req.flash('errorMessage', 'グループ名の更新に失敗しました。');
|
|
|
- return res.redirect('/admin/user-group-detail/' + userGroupData.name);
|
|
|
+ else {
|
|
|
+ // 名前存在チェック
|
|
|
+ return UserGroup.isRegisterableName(name)
|
|
|
+ .then((isRegisterableName) => {
|
|
|
+ // 既に存在するグループ名に更新しようとした場合はエラー
|
|
|
+ if (!isRegisterableName) {
|
|
|
+ req.flash('errorMessage', 'グループ名が既に存在します。');
|
|
|
}
|
|
|
else {
|
|
|
- req.flash('successMessage', 'グループ名を更新しました。');
|
|
|
- return res.redirect('/admin/user-group-detail/' + name);
|
|
|
+ return userGroupData.updateName(name)
|
|
|
+ .then(() => {
|
|
|
+ req.flash('successMessage', 'グループ名を更新しました。');
|
|
|
+ })
|
|
|
+ .catch((err) => {
|
|
|
+ req.flash('errorMessage', 'グループ名の更新に失敗しました。');
|
|
|
+ });
|
|
|
}
|
|
|
});
|
|
|
- });
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .then(() => {
|
|
|
+ return res.redirect('/admin/user-group-detail/' + userGroupData.name);
|
|
|
});
|
|
|
};
|
|
|
|
|
|
@@ -669,7 +664,8 @@ module.exports = function(crowi, app) {
|
|
|
fileUploader.uploadFile(filePath, tmpFile.mimetype, tmpFileStream, {})
|
|
|
.then(function (data) {
|
|
|
var imageUrl = fileUploader.generateUrl(filePath);
|
|
|
- userGroupData.updateImage(imageUrl, function (err, data) {
|
|
|
+ userGroupData.updateImage(imageUrl)
|
|
|
+ .then(() => {
|
|
|
fs.unlink(tmpPath, function (err) {
|
|
|
if (err) {
|
|
|
debug('Error while deleting tmp file.', err);
|
|
|
@@ -697,16 +693,33 @@ module.exports = function(crowi, app) {
|
|
|
actions.userGroup.deletePicture = function (req, res) {
|
|
|
|
|
|
var userGroupId = req.params.userGroupId;
|
|
|
+ let userGroupName = null;
|
|
|
|
|
|
- UserGroup.findById(userGroupId, function (err, userGroupData) {
|
|
|
- if (!userGroupData) {
|
|
|
- req.flash('errorMessage', 'Error while deleting group picture');
|
|
|
+ UserGroup.findById(userGroupId)
|
|
|
+ .then((userGroupData) => {
|
|
|
+ if (userGroupData == null) {
|
|
|
+ return Promise.reject();
|
|
|
}
|
|
|
+ else {
|
|
|
+ userGroupName = userGroupData.name;
|
|
|
+ return userGroupData.deleteImage();
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .then((updated) => {
|
|
|
+ req.flash('successMessage', 'Deleted group picture');
|
|
|
|
|
|
- userGroupData.deleteImage(function (err, data) {
|
|
|
- req.flash('successMessage', 'Deleted group picture');
|
|
|
- });
|
|
|
- return res.redirect('/admin/user-group-detail/' + userGroupData.name);
|
|
|
+ return res.redirect('/admin/user-group-detail/' + userGroupName);
|
|
|
+ })
|
|
|
+ .catch((err) => {
|
|
|
+ debug('An error occured.', err);
|
|
|
+
|
|
|
+ req.flash('errorMessage', 'Error while deleting group picture');
|
|
|
+ if (userGroupName == null) {
|
|
|
+ return res.redirect('/admin/user-groups/');
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return res.redirect('/admin/user-group-detail/' + userGroupName);
|
|
|
+ }
|
|
|
});
|
|
|
};
|
|
|
|