How to add currentUser to scope using angular-fullstack - angularjs

I am using angular-fullstack which comes packed with Authentication already set up. there is a handy function getCurrentUser() which allows me to do something like:
<input ng-bind="getCurrentUser.name"/>
And I will get the name of the active user.
What I would like to do is attach the users name to another object in my scope. Something like this:
$scope.activeUser = $scope.getCurrentUser();
However I am not sure how to go about joining active user with the getCurrentUser() function.

Define activeUser as an array of elements then add only the ids. When needed do something like this:
$scope.activeUser = [];
if (isActive()) {
var currentUser = $scope.getCurrentUser();
$scope.activeUser.push(currentUser.id);
}
isActive() is a function you should create to define if the user is active or not.
Then you need another condition or a "else" to remove user isn't active anymore.
EDIT: of course you can add the entire object if needed to the array you do that with:
$scope.activeUser[currentUser.id] = currentUser;
Then you can get it back on the view with:
<span>{{ activeUser[TheId] }}</span

Related

Adding multiple owners for Google App Maker records

I need help with Google App Maker data model security. I want to set multiple owners for a single record. Like the current user + the assigned admin + super admin.
I need this because all records can have different owners and super-owners/admins.
I know that we can point google app maker to a field containing record owner's email and we can set that field to the current user at the time of the creation of the record.
record.Owner = Session.getActiveUser().getEmail();
I want to know if it is possible to have field owners or have multiple fields like owner1, owner2 and then assign access levels to owner1, owner2...
Or how can we programmatically control the access/security/permissions of records?
The solution I'd use for this one definitely involves a field on the record that contains a comma separated string of all the users who should have access to it. I've worked on the following example to explain better what I have in mind.
I created a model and is called documents and looks like this:
In a page, I have a table and a button to add new document records. The page looks like this:
When I click on the Add Document button, a dialog pops up and looks like this:
The logic on the SUBMIT button on the form above is the following:
widget.datasource.item.owners = app.user.email;
widget.datasource.createItem(function(){
app.closeDialog();
});
That will automatically assign the creator of the record the ownership. To add additional owners, I do it on an edit form. The edit form popus up when I click the edit button inside the record row. It looks like this:
As you can see, I'm using a list widget to control who the owners are. For that, it is necessary to use a <List>String custom property in the edit dialog and that will be the datasource of the list widget. In this case, I've called it owners. I've applied the following to the onClick event of the edit button:
var owners = widget.datasource.item.owners;
owners = owners ? owners.split(",") : [];
app.pageFragments.documentEdit.properties.owners = owners;
app.showDialog(app.pageFragments.documentEdit);
The add button above the list widget has the following logic for the onClick event handler:
widget.root.properties.owners.push("");
The TextBox widget inside the row of the list widget has the following logic for the onValueEdit event handler:
widget.root.properties.owners[widget.parent.childIndex] = newValue;
And the CLOSE button has the following logic for the onClick event handler:
var owners = widget.root.properties.owners || [];
if(owners && owners.length){
owners = owners.filter(function(owner){
return owner != false; //jshint ignore:line
});
}
widget.datasource.item.owners = owners.join();
app.closeDialog();
Since I want to create a logic that will load records only for authorized users, then I had to use a query script in the datasource that will serve that purpose. For that I created this function on a server script:
function getAuthorizedRecords(){
var authorized = [];
var userRoles = app.getActiveUserRoles();
var allRecs = app.models.documents.newQuery().run();
if(userRoles.indexOf(app.roles.Admins) > -1){
return allRecs;
} else {
for(var r=0; r<allRecs.length; r++){
var rec = allRecs[r];
if(rec.owners && rec.owners.indexOf(Session.getActiveUser().getEmail()) > -1){
authorized.push(rec);
}
}
return authorized;
}
}
And then on the documents datasource, I added the following to the query script:
return getAuthorizedRecords();
This solution will load all records for admin users, but for non-admin users, it will only load records where their email is located in the owners field of the record. This is the most elegant solution I could come up with and I hope it serves your purpose.
References:
https://developers.google.com/appmaker/models/datasources#query_script
https://developers.google.com/appmaker/ui/binding#custom_properties
https://developers.google.com/appmaker/ui/logic#events
https://developers-dot-devsite-v2-prod.appspot.com/appmaker/scripting/api/client#Record

Listing Form Templates within another Controller view

I have a form_templates table and a forms table. They're connected by form_template_id. I want to be able to list the form_templates that have been created by title within a select.ctp file I have created within the Forms controller. Just wanting some direction on how to do this with cakephp?
At the moment I have the following code within my FormsController:
public function select()
{
$this->set('page_heading', 'Current Forms');
$contain = [];
$formTemplate = $this->FormTemplates->Forms->find('list', ['order' => 'title'])->where(['active'=>true]);
$forms = $this->paginate($this->Forms->FormTemplates);
$this->set(compact('forms', 'formTemplate'));
}
But I am getting a Call to a member function find() on null error.
Any help on how to tackle this would be greatly appreciated. I know it would be simple but I am new to cakephp.
In your FormsController only FormsTable is loaded automatically, and you are trying to access model that is not currently loaded:
$formTemplate = $this->FormTemplates->Forms->find(...
To get what you want, you should access associated FormTemplatesTable like this:
$formTemplate = $this->Forms->FormTemplates->find(...

How to get Entry Id for a Newly Created Entity in Breeze

i want to create a registration form that will be in batch with a continuation button, getting the id of the entry will help me to call the save method.
I want to immediately get the primary key of a new Entry Created using BreezeJS, Pls i need help on this.
Thanks
Not entirely sure I understand your question, but it sounds like you want to get the id of a newly saved record immediately after the save. If so then the answer below applies.
When the save promise resolves it returns both the list of saved entities as well as a keyMappings array for any entities whose ids changed as a result of the save. i.e. a mapping from temporary to real ids. i.e. (Documented here: http://www.breezejs.com/sites/all/apidocs/classes/EntityManager.html#method_saveChanges)
myEntityManager.saveChanges().then(function (saveResult) {
// entities is an array of entities that were just saved.
var entitites = saveResult.entities;
var keyMappings = saveResult.keyMappings;
keyMappings.forEach(function(km) {
var tempId = km.tempValue;
var newId = km.realValue;
});
});
On the other hand if you have an entity and you just want its 'key' you can use the EntityAspect.getKey method. (see http://www.breezejs.com/sites/all/apidocs/classes/EntityAspect.html#method_getKey)
// assume order is an order entity attached to an EntityManager.
var entityKey = order.entityAspect.getKey();

How to load a user profile using entity_metadata_wrapper in Drupal 7

I am trying to get the user profile loaded with entity_metadata_wrapper:
$user_profile = entity_metadata_wrapper('user', $uid);
dpm($user_profile);
but I get nothing back. The user fields load fine with user_load($uid); What am I doing wrong?
The information provided by Linas is provably incorrect. The use of entity_metadata_wrapper with an entity id is completely okay. The function uses uid internally only if you pass an object to it to begin with, otherwise it passes the argument directly to EntityDrupalWrapper.
Eventually the set method will be called, which states: "Overridden to support setting the entity by either the object or the id."
This basic code will demonstrate a working call to entity_metadata_wrapper with just a user id.
$wrapper = entity_metadata_wrapper('user', 1);
dpm($wrapper->value());
The output will be an array of all the data belonging to the admin user.
The problem you are having is unrelated to your arguments being passed in (assuming $uid is valid), and requires more information to troubleshoot.
entity_metadata_wrapper expects second parameter to be an object, in this case, user object. According to your variable naming, it seems that you are passing user id instead. You can use user_load to fetch user object by ID.
I have the same problem the code not work return empty (Object) EntityDrupalWrapper
global $user;
$user_profile = entity_metadata_wrapper('user', $user->uid);
dpm($user_profile);
But that code work well and return (Object) stdClass with all users fields like name password and other
global $user;
$user = user_load($user->uid);
$wrapper = entity_metadata_wrapper('user', $user);
dpm($wrapper->value());

"at" sign in parameter names in resource definition

from the documentation (http://docs.angularjs.org/api/ngResource.$resource):
$resource(url[, paramDefaults][, actions]);
paramDefaults(optional) – {Object=} – Default values for url parameters.
...
If the parameter value is prefixed with # then the value of that parameter is extracted from the data object.
The question is what data object do they refer to? How to use this feature?
lets say you have a resource like this:
var User = $resource('/user/:userId', {userId:'#id'});
var user = User.get({userId:123});
It means that the value of :userId in your url will be replaced with the id property from the user object when that property is required.
So when is it required? Its required when you are doing something to an existing user, like geting one, updating one. It is not required when you create a user.
In most cases, you will want to have at least one param prefixed with # in your REST url that resource uses (probably the object id). If you dont have one, that means that in order for you to save an instance of an object, you dont need to know anything about where its stored. This implies that its a singleton object. Maybe like a settings object.
Here is your long awaited example:
var User = $resource('/user/:userId/:dogName', {userId:'#id', dogName:#dog});
User.get({userId:123, dog:'Matt'}, function() { .. })
will produce the request: GET /user/123/Matt

Resources