I have a model called Program
, which has the following structure:
var ProgramSchema = new Schema({
permissions: [{
user: {
type: Schema.ObjectId,
ref: 'User'
},
roles: {
type: [{
type: String,
enum: ['reader', 'editor', 'admin', 'requested']
}]
}
}],
type: {
type: String,
enum: ['public', 'private'],
default: 'public',
required: 'Type cannot be blank'
}
});
When displaying a list of programs
on a page, I want to show an icon if the currently authenticated user ($scope.authentication.user
) is in the program.permissions.user
with a role of reader, editor, or admin. I tried using ng-show
but since programs.permissions
is an array, it didn't work as expected. Any suggestions would be appreciated. Thank you!
Here is a sample data for a program
:
{
"_id" : ObjectId("55ab4acd24640cd55097c356"),
"permissions" : [
{
"user" : ObjectId("55a897dfad783baa677e1326"),
"roles" : [
"reader"
]
},
{
"user" : ObjectId("5563f65a84426d913ae8334e"),
"roles" : [
"editor"
]
}
]
}
With some help, here is what I implemented:
I added a function call in my ng-show
:
ng-show="userFollowedProgram(program)"
In my controller:
$scope.userFollowedProgram = function(program) {
// Loop through permissions and check if user is present.
for (var i = 0; i < program.permissions.length; i++) {
if (program.permissions[i].user === $scope.authentication.user._id) {
// Check if user has admin, editor, or reader role.
if (program.permissions[i].roles.indexOf('admin') > -1 ||
program.permissions[i].roles.indexOf('editor') > -1 ||
program.permissions[i].roles.indexOf('reader') > -1) {
return true;
}
}
}
return false;
};