Skip to content
Lance Pollard edited this page Sep 24, 2012 · 2 revisions

Authentication

Model

class User extends Metro.Model
  @embedsMany "authorizations"
  
  @findFromSession: (session) ->
    token = session?.cookies["connect.sid"]
    return unless token
    
    @where(persistentToken: token).first()
  
class Authorization extends Metro.Model
  @embeddedIn "user"

Controller

class ApplicationController extends Metro.Controller
  @beforeFilter "currentUser"
  
  currentUser: ->
    @currentUser ||= User.findFromSession(@session)
  
class AuthorizationsController extends ApplicationController
  create: ->
    @user = User.create(params.auth)
    
class SessionsController extends Metro.Controller
  

Some links on everyauth integration

  • (everyauth config example)[https://github.com/viatropos/tower/issues/18#issuecomment-4278945]
  • (init)[https://gist.github.com/2318237]
  • (app)[https://gist.github.com/2318167]
  • (User)[https://gist.github.com/2318251]

Authorization

class Ability extends CanCan.Ability
  constructor: (user) ->
    if @user.admin
      @can "update", Article
    
    @can "read", Article

Views

- if can "read", Article
  ul
    li Articles!
    
- if can "update", Article
  input(type='submit', value='Save')
Clone this wiki locally