0

How to get user object in abilities source file in Ember-can addon. This is how my abilities file looks like.

import Ember from 'ember';
import { Ability } from 'ember-can';

export default Ability.extend({
    canWrite: Ember.computed('user.isAdmin', function() {
       return this.get('user.isAdmin');
    })
});
Vivekraj K R
  • 2,418
  • 2
  • 19
  • 38

1 Answers1

2

According to the official documentation:

Injecting the user

How does the ability know who's logged in? This depends on how you implement it in your app!

If you're using an Ember.Service as your session, you can just inject it into the ability:

// app/abilities/foo.js
import Ember from 'ember';
import { Ability } from 'ember-can';

export default Ability.extend({
  session: Ember.inject.service()
});

If you're using ember-simple-auth, you'll probably want to inject the simple-auth-session:main session into the ability classes.

To do this, add an initializer like so:

// app/initializers/inject-session-into-abilities.js
export default {
  name: 'inject-session-into-abilities',
  initialize(app) {
    app.inject('ability', 'session', 'simple-auth-session:main');
  }
};

The ability classes will now have access to session which can then be used to check if the user is logged in etc...

Samuel Herzog
  • 3,561
  • 1
  • 22
  • 21
  • how can i get the user object via web service within the ability class? – Vivekraj K R Jan 20 '17 at 10:54
  • I think that's something you actually shouldn't do to keep it DRY... Use a Service to get your user object via web service, and inject that service to your ability class. – Samuel Herzog Jan 20 '17 at 16:03
  • How to inject that user object in ability class? – Vivekraj K R Jan 23 '17 at 05:57
  • With DI provided by Ember: https://guides.emberjs.com/v2.10.0/applications/dependency-injection/#toc_ad-hoc-injections – Samuel Herzog Jan 23 '17 at 14:56
  • You might want to give this series of screencasts a try, explaining in a lot more detail: https://www.emberscreencasts.com/tags/user-auth – Samuel Herzog Jan 23 '17 at 14:58
  • Thanks for your help.finally i'm able to inject the user properties with session itself.but arise with another problem http://stackoverflow.com/questions/41825811/ember-js-how-to-pass-a-string-as-an-argument-in-ember-can-helper – Vivekraj K R Jan 24 '17 at 10:37