 AccountsUtil =
+
+  AUTHOR_FIELD: 'author'
 
   addCollectionAuthorization: (collection, options) ->
     options = _.extend({
     }, options)
+    AUTHOR_FIELD = @AUTHOR_FIELD
     if Meteor.isServer
       name = Collections.getName(collection)
       # Only publish documents belonging to the logged in user.

      catch e
    user

# Set up role publications.

if Meteor.isServer

          if options.userSelector
            selector = options.userSelector(userId: userId, user: user, username: username)
          else
            selector = {}
            selector[AUTHOR_FIELD] = username
          collection.find(selector)

    # Add the logged in user as the author when a doc is created in the collection.
    collection.before.insert (userId, doc) ->
      user = Meteor.users.findOne(userId)
      author = doc[AUTHOR_FIELD] ?= user?.username
      unless author
        suffix = ' when inserting doc in collection ' + Collections.getName(collection)
        if !user?
          throw new Error('User not provided' + suffix)
        else if !user.username?
          throw new Error('No username provided' + suffix)
        else
          throw new Error('No author provided' + suffix)
  
  isOwner: (doc, user) ->
    user = @_resolveUser(user)
    doc[@AUTHOR_FIELD] == user?.username

  isOwnerOrAdmin: (doc, user) -> @isOwner(doc, user) || @isAdmin(user)

  isAuthorized: (doc, user, predicate) ->
    user = @_resolveUser(user)
    if predicate
      predicate(doc, user)
    else
      isOwnerOrAdmin(user)

  authorize: (doc, user, predicate) ->
    unless @isAuthorized(doc, user, predicate)
      throw new Meteor.Error(403, 'Access denied')

  isAdmin: (user) ->
