├── .gitignore ├── .travis.yml ├── .versions ├── LICENSE ├── README.md ├── lib.coffee ├── package.js ├── server.coffee └── tests.coffee /.gitignore: -------------------------------------------------------------------------------- 1 | .build* 2 | .idea 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | sudo: required 5 | before_install: 6 | - "curl -L http://git.io/ejPSng | /bin/sh" 7 | -------------------------------------------------------------------------------- /.versions: -------------------------------------------------------------------------------- 1 | accounts-base@1.4.3 2 | accounts-password@1.5.1 3 | allow-deny@1.1.0 4 | babel-compiler@7.3.4 5 | babel-runtime@1.3.0 6 | base64@1.0.11 7 | binary-heap@1.0.11 8 | boilerplate-generator@1.6.0 9 | caching-compiler@1.2.1 10 | callback-hook@1.1.0 11 | check@1.3.1 12 | coffeescript@2.4.1 13 | coffeescript-compiler@2.4.1 14 | ddp@1.4.0 15 | ddp-client@2.3.3 16 | ddp-common@1.4.0 17 | ddp-rate-limiter@1.0.7 18 | ddp-server@2.3.0 19 | diff-sequence@1.1.1 20 | dynamic-import@0.5.1 21 | ecmascript@0.12.7 22 | ecmascript-runtime@0.7.0 23 | ecmascript-runtime-client@0.8.0 24 | ecmascript-runtime-server@0.7.1 25 | ejson@1.1.0 26 | email@1.2.3 27 | fetch@0.1.1 28 | geojson-utils@1.0.10 29 | id-map@1.1.0 30 | inter-process-messaging@0.1.0 31 | local-test:peerlibrary:user-extra@0.5.0 32 | localstorage@1.2.0 33 | logging@1.1.20 34 | meteor@1.9.3 35 | minimongo@1.4.5 36 | modern-browsers@0.1.4 37 | modules@0.13.0 38 | modules-runtime@0.10.3 39 | mongo@1.6.2 40 | mongo-decimal@0.1.1 41 | mongo-dev-server@1.1.0 42 | mongo-id@1.0.7 43 | npm-bcrypt@0.9.3 44 | npm-mongo@3.1.2 45 | ordered-dict@1.1.0 46 | peerlibrary:classy-test@0.4.0 47 | peerlibrary:extend-publish@0.6.0 48 | peerlibrary:publish-context@0.7.0 49 | peerlibrary:user-extra@0.5.0 50 | promise@0.11.2 51 | random@1.1.0 52 | rate-limit@1.0.9 53 | reactive-var@1.0.11 54 | reload@1.3.0 55 | retry@1.1.0 56 | routepolicy@1.1.0 57 | service-configuration@1.0.11 58 | sha@1.0.9 59 | socket-stream-client@0.2.2 60 | srp@1.0.12 61 | tinytest@1.1.0 62 | tracker@1.2.0 63 | underscore@1.0.10 64 | webapp@1.7.3 65 | webapp-hashing@1.0.9 66 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, The PeerLibrary Project 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | * Redistributions of source code must retain the above copyright 7 | notice, this list of conditions and the following disclaimer. 8 | * Redistributions in binary form must reproduce the above copyright 9 | notice, this list of conditions and the following disclaimer in the 10 | documentation and/or other materials provided with the distribution. 11 | * Neither the name of the PeerLibrary Project nor the 12 | names of its contributors may be used to endorse or promote products 13 | derived from this software without specific prior written permission. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY 19 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Extended Meteor.userId() and Meteor.user() 2 | ========================================== 3 | 4 | Adding this package to your [Meteor](http://www.meteor.com/) application: 5 | 6 | * makes [`Meteor.userId()`](http://docs.meteor.com/#/full/meteor_userid) 7 | work also inside the publish endpoint functions (even for Meteor versions prior to 1.5.1) 8 | * prevents direct modification of the user's profile from the client, which 9 | is otherwise allowed by Meteor 10 | * extends [`Meteor.user(userId)`](http://docs.meteor.com/#/full/meteor_user) 11 | into `Meteor.user(userId, fields)` which now accepts an argument to limit 12 | queried fields (and thus function's reactivity); `userId` is optional and if 13 | not specified `Meteor.userId()` is used instead 14 | 15 | Both client and server side. 16 | 17 | Installation 18 | ------------ 19 | 20 | ``` 21 | meteor add peerlibrary:user-extra 22 | ``` 23 | 24 | Examples 25 | -------- 26 | 27 | ```javascript 28 | Template.user.helpers({ 29 | username: function () { 30 | var user = Meteor.user({username: 1}); 31 | return user && user.username; 32 | } 33 | }); 34 | ``` 35 | -------------------------------------------------------------------------------- /lib.coffee: -------------------------------------------------------------------------------- 1 | Meteor.user = (userId, fields) -> 2 | if not fields and _.isObject userId 3 | fields = userId 4 | userId = null 5 | 6 | # Meteor.userId is reactive. 7 | userId ?= Meteor.userId() 8 | fields ?= {} 9 | 10 | return null unless userId 11 | 12 | Meteor.users.findOne 13 | _id: userId 14 | , 15 | fields: fields 16 | -------------------------------------------------------------------------------- /package.js: -------------------------------------------------------------------------------- 1 | Package.describe({ 2 | name: 'peerlibrary:user-extra', 3 | summary: "Extended Meteor.userId() and Meteor.user()", 4 | version: '0.5.0', 5 | git: 'https://github.com/peerlibrary/meteor-user-extra.git' 6 | }); 7 | 8 | Package.onUse(function (api) { 9 | api.versionsFrom('METEOR@1.8.1'); 10 | 11 | // Core dependencies. 12 | api.use([ 13 | 'coffeescript@2.4.1', 14 | 'ecmascript', 15 | 'ddp', 16 | 'accounts-base', 17 | 'underscore' 18 | ]); 19 | 20 | // 3rd party dependencies. 21 | api.use([ 22 | 'peerlibrary:publish-context@0.7.0' 23 | ], 'server'); 24 | 25 | api.addFiles([ 26 | 'server.coffee' 27 | ], 'server'); 28 | 29 | api.addFiles([ 30 | 'lib.coffee' 31 | ]); 32 | }); 33 | 34 | Package.onTest(function (api) { 35 | api.versionsFrom('METEOR@1.8.1'); 36 | 37 | // Core dependencies. 38 | api.use([ 39 | 'coffeescript@2.4.1', 40 | 'ecmascript', 41 | 'accounts-password', 42 | 'random', 43 | 'mongo' 44 | ]); 45 | 46 | // Internal dependencies. 47 | api.use([ 48 | 'peerlibrary:user-extra' 49 | ]); 50 | 51 | // 3rd party dependencies. 52 | api.use([ 53 | 'peerlibrary:classy-test@0.4.0' 54 | ]); 55 | 56 | api.addFiles([ 57 | 'tests.coffee' 58 | ]); 59 | }); 60 | -------------------------------------------------------------------------------- /server.coffee: -------------------------------------------------------------------------------- 1 | # If Meteor < 1.5.1. 2 | if not DDP._CurrentPublicationInvocation 3 | Meteor.userId = -> 4 | currentInvocation = DDP._CurrentInvocation.get() 5 | 6 | return currentInvocation.userId if currentInvocation 7 | 8 | currentContext = DDP._CurrentPublish.get() 9 | 10 | return currentContext.userId if currentContext 11 | 12 | throw new Error "Meteor.userId() not invoked from a method or publish function." 13 | 14 | # Forbid users from making any modifications to their user document. 15 | Meteor.users.deny 16 | update: -> 17 | true 18 | -------------------------------------------------------------------------------- /tests.coffee: -------------------------------------------------------------------------------- 1 | if Meteor.isServer 2 | Meteor.publish 'userExtraTest', -> 3 | @added 'testCollection', Random.id(), 4 | userId: Meteor.userId() 5 | @ready() 6 | 7 | else 8 | TestCollection = new Mongo.Collection 'testCollection' 9 | 10 | class BasicTestCase extends ClassyTestCase 11 | @testName: 'user-extra - basic' 12 | 13 | setUpServer: -> 14 | Meteor.users.remove 15 | username: 'test-user-extra' 16 | 17 | tearDownServer: -> 18 | Meteor.users.remove 19 | username: 'test-user-extra' 20 | 21 | testClientBasic: [ 22 | -> 23 | @assertSubscribeSuccessful 'userExtraTest', @expect() 24 | , 25 | -> 26 | @assertEqual Meteor.userId(), null 27 | @assertEqual TestCollection.find({}, fields: {_id: 0}).fetch(), [userId: null] 28 | , 29 | -> 30 | Accounts.createUser 31 | username: 'test-user-extra' 32 | password: 'test' 33 | email: 'test@example.com' 34 | , 35 | @expect (error) => 36 | @assertFalse error, error 37 | , 38 | @runOnServer -> 39 | @assertTrue Meteor.userId() 40 | 41 | @set 'userId', Meteor.userId() 42 | , 43 | -> 44 | @assertEqual Meteor.userId(), @get 'userId' 45 | @assertEqual TestCollection.find({}, fields: {_id: 0}).fetch(), [userId: Meteor.userId()] 46 | , 47 | -> 48 | Meteor.users.update Meteor.userId(), 49 | $set: 50 | profile: 51 | name: 'test' 52 | , 53 | @expect (error, changes) => 54 | # Updating the user profile should not be allowed. 55 | @assertTrue error, error 56 | , 57 | -> 58 | @assertTrue Meteor.user().emails 59 | @assertFalse Meteor.user({username: 1}).emails 60 | , 61 | -> 62 | Accounts.logout @expect (error) => 63 | @assertFalse error, error 64 | , 65 | -> 66 | @assertEqual Meteor.userId(), null 67 | @assertEqual TestCollection.find({}, fields: {_id: 0}).fetch(), [userId: null] 68 | ] 69 | 70 | ClassyTestCase.addTest new BasicTestCase() 71 | --------------------------------------------------------------------------------