Skip to content

Instantly share code, notes, and snippets.

@joepie91
Created December 25, 2014 21:54
Show Gist options
  • Save joepie91/cc0a7f4c1f4e6974d903 to your computer and use it in GitHub Desktop.
Save joepie91/cc0a7f4c1f4e6974d903 to your computer and use it in GitHub Desktop.
Promise = require "bluebird"
appUtil = require "../util"
module.exports = (shelf) ->
shelf.model "User",
tableName: "users"
hasTimestamps: true
projects: -> @hasMany "Project", "owner_id"
enable: ->
if @get "activated"
throw new util.errors.RequirementsError("The user is already enabled.")
else
@set "activated", true
@saveChanges()
disable: ->
if not @get "activated"
throw new util.errors.RequirementsError("The user is already disabled.")
else
@set "activated", false
@saveChanges()
verifyPassword: (password) ->
return appUtil.verifyHash @get("hash"), password
setPassword: (password) ->
Promise.try ->
appUtil.hash password
.then (hash) =>
@set "hash", hash
@saveChanges()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment