Skip to content

Commit 9bda1fb

Browse files
converted to coffee
1 parent fce46e4 commit 9bda1fb

File tree

11 files changed

+148
-85
lines changed

11 files changed

+148
-85
lines changed

README.md

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ Based on: http://avitevet.blogspot.com.es/2013/01/shelving-emberjs-was-authoriza
66

77
And http://livsey.org/blog/2012/10/16/writing-a-helper-to-check-permissions-in-ember-dot-js/
88

9-
We should also include stuff from here: http://livsey.org/blog/2012/02/23/should-your-user-care-about-authentication/
9+
Also includes from here: http://livsey.org/blog/2012/02/23/should-your-user-care-about-authentication/
1010

1111
## Note
1212

13-
Work in progress... please help improve it!
13+
This gem is a work in progress... please help improve it!
1414

1515
## Installation
1616

@@ -40,10 +40,41 @@ App.Routes.ApiUrl = {
4040
};
4141
```
4242

43+
## Ember Auth toolset
44+
45+
Simply add the following to your script manifest.
46+
47+
```coffeescript
48+
#= require beercan
49+
```
50+
51+
Or alternatively include individual "modules"
52+
53+
```coffeescript
54+
#= require beercan/authorization
55+
```
56+
57+
Add permissions:
58+
59+
```javascript
60+
App.Permissions.register("createPost", App.Permission.extend({
61+
can: function() {
62+
return this.get("currentUser.isAdmin");
63+
}.property("currentUser.isAdmin")
64+
}));
65+
66+
App.Permissions.register("editPost", App.Permission.extend({
67+
can: function(){
68+
return this.get("currentUser.isAdmin") || this.get("content.author.id") == this.get("currentUser.id");
69+
}.property("currentUser.isAdmin", "content")
70+
}));
71+
```
72+
4373
## Controllers
4474

45-
* AuthorizationsController
46-
* TokensController
75+
* AuthorizationsController (subclass to have your control authorization enabled)
76+
* TokensController (for managing auth tokens)
77+
* GuardedController
4778

4879
### Doorkeeper
4980

lib/ember/beercan/user_access.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
module UserAccess
2+
AUTH_TYPES = [
3+
FaceBook,
4+
Twitter,
5+
GitHub,
6+
Password
7+
]
8+
9+
def self.authenticate(params)
10+
AUTH_TYPES.detect{|auth| auth.can_handle?(params) }.try(:authenticate, params)
11+
end
12+
13+
module FaceBook
14+
def self.can_handle?(params)
15+
params.has_key?(:facebook_id)
16+
end
17+
18+
def self.authenticate(params)
19+
end
20+
end
21+
22+
module Google
23+
def self.can_handle?(params)
24+
params.has_key?(:google_id)
25+
end
26+
27+
def self.authenticate(params)
28+
end
29+
end
30+
31+
module Twitter
32+
def self.can_handle?(params)
33+
params.has_key?(:twitter_id)
34+
end
35+
36+
def self.authenticate(params)
37+
end
38+
end
39+
40+
module Password
41+
def self.can_handle?(params)
42+
params.has_key?(:username) && params.has_key?(:password)
43+
end
44+
45+
def self.authenticate(params)
46+
end
47+
end
48+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#= require beercan/authorization
2+
#= require beercan/authentication
3+
4+
# ... more
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#= require_tree authorization

vendor/assets/javascripts/beercan/authorization/permission.js renamed to vendor/assets/javascripts/beercan/authorization/permission.js.coffee

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
App.Permission = Ember.Object.extend({
1+
App.Permission = Ember.Object.extend
22
content: null,
33
currentUserBinding: "App.currentUser"
4-
});

vendor/assets/javascripts/beercan/authorization/permissions.js

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
App.Authorization.Permissions =
2+
_perms: {}
3+
register: (name, klass) ->
4+
@_perms[name] = klass
5+
6+
get: (name, attrs) ->
7+
@_perms[name].create(attrs)
8+
9+
can: (name, attrs) ->
10+
@get(name, attrs).get("can")

vendor/assets/javascripts/beercan/helpers/beer_can_helper.js

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
App.BeerCan = Ember.Object.extend
2+
init: (context, property, options) ->
3+
@context = context
4+
@property = property
5+
@options = options
6+
7+
getProp: ->
8+
if Ember.isGlobalPath(property)
9+
Ember.get(property);
10+
else
11+
Ember.get(@path.root, path.path)
12+
13+
path: ->
14+
@normalizedPath ||= Ember.Handlebars.normalizePath context, property, options.data
15+
16+
# TODO: Refactor!!!
17+
Handlebars.registerHelper 'can', (permissionName, property, options) ->
18+
# property is optional, if we've only got 2 arguments then the property contains our options
19+
if !options
20+
options = property
21+
property = null
22+
23+
context = (options.contexts && options.contexts[0]) || @
24+
25+
beerCan = App.BeerCan.create context, property, options
26+
27+
attrs = {}
28+
29+
# if we've got a property name, get its value and set it to the permission's content
30+
# this will set the passed in `post` to the content eg:
31+
# {{#can editPost post}} ... {{/can}}
32+
33+
attrs.content = beerCan.getProp() if property
34+
35+
# if we've got any options, find their values eg:
36+
# {{#can createPost project:Project user:App.currentUser}} ... {{/can}}
37+
for (key in options.hash)
38+
beerCan.property = options.hash[key]
39+
attrs[key] = beerCan.getProp()
40+
41+
# find & create the permission with the supplied attributes
42+
permission = App.Authorization.Permissions.get permissionName, attrs
43+
44+
# ensure boundIf uses permission as context and not the view/controller
45+
# otherwise it looks for 'can' in the wrong place
46+
options.contexts = null
47+
48+
# bind it all together and kickoff the observers
49+
return Ember.Handlebars.helpers.boundIf.call permission, "can", options

vendor/assets/javascripts/beercan/helpers/can_helper.js.coffee

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)