extraFragment -> fragment; deprecate Users.canEdit; add alias; use new withCurrentUser

This commit is contained in:
Sacha Greif 2017-01-12 17:30:25 +09:00
parent 7f99b48953
commit 171bf9feb9
12 changed files with 62 additions and 54 deletions

View file

@ -21,6 +21,7 @@
"formsy-react": "^0.18.1",
"formsy-react-components": "^0.8.1",
"graphql": "^0.8.2",
"graphql-date": "^1.0.2",
"graphql-server-express": "^0.4.4",
"graphql-subscriptions": "^0.2.1",
"graphql-tag": "^1.2.1",

View file

@ -1,4 +1,4 @@
import { Components, registerComponent } from 'meteor/nova:lib';
import { Components, registerComponent, getRawComponent } from 'meteor/nova:core';
import React, { PropTypes, Component } from 'react';
import SmartForm from "meteor/nova:forms";
import Comments from "meteor/nova:comments";
@ -15,16 +15,7 @@ const CommentsEditForm = (props, context) => {
cancelCallback={props.cancelCallback}
removeSuccessCallback={props.removeSuccessCallback}
showRemove={true}
extraFragment={`
htmlBody
postedAt
user{
_id
__displayName
__emailHash
__slug
}
`}
fragment={getRawComponent('PostsCommentsThread').fragment}
/>
</div>
)

View file

@ -69,6 +69,7 @@ const options = {
collection: Comments,
queryName: 'commentsListQuery',
fragment: PostsCommentsThread.fragment,
limit: 0,
};
registerComponent('PostsCommentsThread', PostsCommentsThread, withList(options), withCurrentUser);

View file

@ -1,4 +1,4 @@
import { Components, registerComponent } from 'meteor/nova:lib';
import { Components, registerComponent, getRawComponent } from 'meteor/nova:core';
import React, { PropTypes, Component } from 'react';
import { intlShape } from 'react-intl';
import SmartForm from "meteor/nova:forms";
@ -27,16 +27,7 @@ class PostsEditForm extends Component {
<SmartForm
collection={Posts}
documentId={this.props.post._id}
extraFragment={`
htmlBody
postedAt
user{
_id
__displayName
__emailHash
__slug
}
`}
fragment={getRawComponent('PostsPage').fragment}
successCallback={post => {
this.props.closeModal();
this.props.flash(this.context.intl.formatMessage({id: "posts.edit_success"}, {title: post.title}), 'success');

View file

@ -1,4 +1,4 @@
import { Components, registerComponent, getRawComponent } from 'meteor/nova:lib';
import { Components, registerComponent, getRawComponent } from 'meteor/nova:core';
import SmartForm from "meteor/nova:forms";
import { ShowIf, withMessages } from 'meteor/nova:core';
import Posts from "meteor/nova:posts";

View file

@ -5,14 +5,6 @@ import { addCallback, Utils } from 'meteor/nova:core';
// ------------------------------------- comments.edit.validate -------------------------------- //
function CommentsEditUserCheck (modifier, comment, user) {
if (!user || !Users.canEdit(user, comment)) {
throw new Meteor.Error(601, 'sorry_you_cannot_edit_this_comment');
}
return modifier;
}
addCallback("comments.edit.validate", CommentsEditUserCheck);
function CommentsEditSubmittedPropertiesCheck (modifier, comment, user) {
const schema = Posts.simpleSchema()._schema;
// go over each field and throw an error if it's not editable

View file

@ -5,10 +5,37 @@ import gql from 'graphql-tag';
import { Utils } from 'meteor/nova:lib';
import Users from 'meteor/nova:users';
const withCurrentUser1 = component => {
const preloadedFields = Users.getPreloadedFields();
return graphql(gql`
query getCurrentUser {
currentUser {
${preloadedFields.join('\n')}
}
}
`, {
options(ownProps) {
return {
alias: 'withCurrentUser',
};
},
props(props) {
const {data: {loading, currentUser}} = props;
return {
loading,
currentUser,
};
},
}
)(component);
}
/**
* withCurrentUser - HOC to give access to the currentUser as a prop of a WrappedComponent
**/
const withCurrentUser = WrappedComponent => {
const withCurrentUser2 = WrappedComponent => {
class WithCurrentUser extends Component {
constructor(...args) {
@ -57,8 +84,6 @@ const withCurrentUser = WrappedComponent => {
return hoistStatics(WithCurrentUser, WrappedComponent);
};
export default withCurrentUser;
/**
************************** old pattern without executing a query **************************
@ -88,3 +113,5 @@ function withCurrentUserWithoutQuery(WrappedComponent) {
return hoistStatics(WithCurrentUser, WrappedComponent);
}
export default withCurrentUser1;

View file

@ -19,6 +19,7 @@ export default function withDocument (options) {
options(ownProps) {
return {
variables: { documentId: ownProps.documentId, slug: ownProps.slug },
alias: 'withDocument',
pollInterval, // note: pollInterval can be set to 0 to disable polling (20s by default)
};
},

View file

@ -82,6 +82,7 @@ const withList = (options) => {
options(ownProps) {
// console.log(ownProps)
return {
alias: 'withList',
variables: {
terms: ownProps.terms,
// note: pollInterval can be set to 0 to disable polling (20s by default)

View file

@ -1,4 +1,5 @@
import { getSetting } from 'meteor/nova:core';
import Posts from 'meteor/nova:posts';
Meteor.methods({
testGetEmbedlyData: function (url) {
@ -14,7 +15,7 @@ Meteor.methods({
},
generateThumbnail: function (post) {
check(post, Posts.simpleSchema());
if (Users.canEdit(Meteor.user(), post)) {
if (Posts.options.mutations.edit.check(Meteor.user(), post)) {
regenerateThumbnail(post);
}
},

View file

@ -14,7 +14,7 @@ Meteor.methods({
'newsletter.addUser'(user){
const currentUser = Users.findOne({_id: this.userId});
user = Users._transform(user);
if (!user || !Users.canEdit(currentUser, user)) {
if (!user || !Users.options.mutations.edit.check(currentUser, user)) {
throw new Meteor.Error(601, 'sorry_you_cannot_edit_this_user');
}
@ -27,7 +27,7 @@ Meteor.methods({
'newsletter.removeUser'(user) {
const currentUser = Users.findOne({_id: this.userId});
user = Users._transform(user);
if (!user || !Users.canEdit(currentUser, user)) {
if (!user || !Users.options.mutations.edit.check(currentUser, user)) {
throw new Meteor.Error(601, 'sorry_you_cannot_edit_this_user');
}

View file

@ -113,34 +113,36 @@ Users.canDo = (user, action) => {
return Users.getActions(user).indexOf(action) !== -1;
};
// DEPRECATED
// TODO: remove this
/**
* @summary Check if a user can edit a document
* @param {Object} user - The user performing the action
* @param {Object} document - The document being edited
*/
Users.canEdit = function (user, document) {
// Users.canEdit = function (user, document) {
user = (typeof user === 'undefined') ? Meteor.user() : user;
// user = (typeof user === 'undefined') ? Meteor.user() : user;
// note(apollo): use of `__typename` given by react-apollo
//const collectionName = document.getCollectionName();
const collectionName = document.__typename ? Utils.getCollectionNameFromTypename(document.__typename) : document.getCollectionName();
// // note(apollo): use of `__typename` given by react-apollo
// //const collectionName = document.getCollectionName();
// const collectionName = document.__typename ? Utils.getCollectionNameFromTypename(document.__typename) : document.getCollectionName();
if (!user || !document) {
return false;
}
// if (!user || !document) {
// return false;
// }
if (document.hasOwnProperty('isDeleted') && document.isDeleted) return false;
// if (document.hasOwnProperty('isDeleted') && document.isDeleted) return false;
if (Users.owns(user, document)) {
// if this is user's document, check if user can edit own documents
return Users.canDo(user, `${collectionName}.edit.own`);
} else {
// if this is not user's document, check if they can edit all documents
return Users.canDo(user, `${collectionName}.edit.all`);
}
// if (Users.owns(user, document)) {
// // if this is user's document, check if user can edit own documents
// return Users.canDo(user, `${collectionName}.edit.own`);
// } else {
// // if this is not user's document, check if they can edit all documents
// return Users.canDo(user, `${collectionName}.edit.all`);
// }
};
// };
/**
* @summary Check if a user owns a document