             User.create ADMIN_USER, cb
 
 
+    ###*
+    Create admin role, called once in 'init' function.
+    @private
+    ###
     createAdminRole: ->
 
         ____("creating admin role.")

                role.principals.create principal, cb



    update: ->

        Promise.resolve @fetchNew()

        .then (tokenStr) =>
            token = new AdminToken(tokenStr)
            @setNew(token)

        .then (succeeded) =>
            @deleteOldest() if succeeded



    setNew: (token) ->

        { AccessToken } = @models

        @exists(token).then (exists) =>

            ____("token: #{token.id} already exists.") if exists
            return false if exists

            ____("saving token: #{token.id}")
            promisify (cb) =>
                AccessToken.create token, cb

            .then (savedToken) =>
                @tokens.push savedToken
                return true



    exists: (token) ->

        { AccessToken } = @models

        promisify (cb) =>
            AccessToken.exists token.id, cb



    deleteOldest: ->

        { AccessToken } = @models

        token = @tokens[0]

        promisify (cb) =>
            AccessToken.destroyById token.id, cb

        .then =>
            @tokens.shift()




class AdminToken

    constructor: (@id) ->
        @userId = ADMIN_USER.id
        @ttl = ONE_YEAR
        @createdAt = new Date().getTime()




        fn(cb)


###*
Admin token manager

@class AdminTokenManager
###
class AdminTokenManager

    ###*
    @param {Function|Array(String)} [options.fetch] function to return admin tokens (or promise of it). When string[] is given, these value are used for the admin access token.
    @param {Number} [options.intervalHours] Interval hours to fetch new admin token.
    ###
    constructor: (options = {}) ->

        { fetch, @intervalHours } = options

        @fetch = @constructor.createFetchFunction(fetch)

        @tokensById = {}



    ###*
    Set fetched tokens as admin tokens.

    @public
    @method init
    @param {Object} models app.models in LoopBack
    @return {Promise}
    ###
    init: (@models) ->

        @createAdminUser()
