Move UserInfoParser to sdkjs

This commit is contained in:
Julia Radzhabova 2021-04-07 18:04:54 +03:00
parent 2c0696ed12
commit 1bd7203489

View file

@ -981,113 +981,3 @@ Common.Utils.ModalWindow = new(function() {
}
}
})();
Common.Utils.UserInfoParser = new(function() {
var parse = false,
separator = String.fromCharCode(160),
username = '',
_reviewPermissions,
reviewGroups,
commentGroups;
var _intersection = function (arr1, arr2) {
if (arr1 && arr2) {
for (var i=0; i<arr2.length; i++) {
if (arr1.indexOf(arr2[i])>-1)
return true;
}
}
return false;
};
var _getParsedGroups = function(username) {
if (parse && username) {
var idx = username.indexOf(separator),
groups = (idx>-1) ? username.substring(0, idx).split(',') : [];
for (var i=0; i<groups.length; i++)
groups[i] = groups[i].trim();
return groups;
}
};
return {
setParser: function(value) {
parse = !!value;
},
setCurrentName: function(name) {
username = name;
_reviewPermissions && this.setReviewPermissions(null, _reviewPermissions); // old version of review permissions
},
getCurrentName: function() {
return username;
},
getSeparator: function() {
return separator;
},
getParsedName: function(username) {
return (parse && username) ? username.substring(username.indexOf(separator)+1) : username;
},
setReviewPermissions: function(groups, permissions) {
if (groups) {
if (typeof groups == 'object' && groups.length>0)
reviewGroups = groups;
} else if (permissions) { // old version of review permissions
var arr = [],
arrgroups = _getParsedGroups(username);
arrgroups && arrgroups.forEach(function(group) {
var item = permissions[group.trim()];
item && (arr = arr.concat(item));
});
reviewGroups = arr;
_reviewPermissions = permissions;
}
},
setCommentPermissions: function(groups) {
if (groups && typeof groups == 'object') {
commentGroups = {
view: (typeof groups.view == 'object' && groups.view.length>0) ? groups.view : null,
edit: (typeof groups.edit == 'object' && groups.edit.length>0) ? groups.edit : null,
remove: (typeof groups.remove == 'object' && groups.remove.length>0) ? groups.remove : null
};
}
},
canEditReview: function(username) {
if (!parse || !reviewGroups) return true;
var groups = _getParsedGroups(username);
groups && (groups.length==0) && (groups = [""]);
return _intersection(reviewGroups, groups);
},
canViewComment: function(username) {
if (!parse || !commentGroups || !commentGroups.view) return true;
var groups = _getParsedGroups(username);
groups && (groups.length==0) && (groups = [""]);
return _intersection(commentGroups.view, groups);
},
canEditComment: function(username) {
if (!parse || !commentGroups || !commentGroups.edit) return true;
var groups = _getParsedGroups(username);
groups && (groups.length==0) && (groups = [""]);
return _intersection(commentGroups.edit, groups);
},
canDeleteComment: function(username) {
if (!parse || !commentGroups || !commentGroups.remove) return true;
var groups = _getParsedGroups(username);
groups && (groups.length==0) && (groups = [""]);
return _intersection(commentGroups.remove, groups);
}
}
})();