Skip to content

Commit

Permalink
part 6 - security
Browse files Browse the repository at this point in the history
  • Loading branch information
hiukim committed Aug 9, 2016
1 parent c861f4b commit b5c9a39
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .meteor/packages
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ [email protected] # JS minifier run for production mode
[email protected] # ECMAScript 5 compatibility for older browsers.
[email protected] # Enable ECMAScript2015+ syntax in app code

[email protected] # Publish all data to the clients (for prototyping)
[email protected] # Allow all DB writes from clients (for prototyping)
react-meteor-data
accounts-password
aldeed:simple-schema
mdg:validated-method
41 changes: 41 additions & 0 deletions imports/api/methods/games.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {GamesController} from "../controllers/gamesController.js";

export const newGame = new ValidatedMethod({
name: 'games.newGame',
validate: new SimpleSchema({}).validator(),
run({}) {
GamesController.newGame(Meteor.user());
}
});

export const userJoinGame = new ValidatedMethod({
name: 'games.userJoinGame',
validate: new SimpleSchema({
gameId: {type: String}
}).validator(),
run({gameId}) {
GamesController.userJoinGame(gameId, Meteor.user());
}
});

export const userLeaveGame = new ValidatedMethod({
name: 'games.userLeaveGame',
validate: new SimpleSchema({
gameId: {type: String}
}).validator(),
run({gameId}) {
GamesController.userLeaveGame(gameId, Meteor.user());
}
});

export const userMarkGame = new ValidatedMethod({
name: 'games.userMarkGame',
validate: new SimpleSchema({
gameId: {type: String},
row: {type: Number},
col: {type: Number}
}).validator(),
run({gameId, row, col}) {
GamesController.userMarkGame(gameId, Meteor.user(), row, col);
}
});
12 changes: 12 additions & 0 deletions imports/api/server/publications.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {GameStatuses} from '../models/game.js';
import Games from '../collections/games.js';

Meteor.publish('games', function() {
// access control: only for loggined-in users
if (this.userId) { // this.userId is the id of the currently loggined in user
// filtering: only games with WAITING and STARTED statuses
return Games.find({status: {$in: [GameStatuses.WAITING, GameStatuses.STARTED]}});
} else {
return null;
}
});
2 changes: 2 additions & 0 deletions imports/ui/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ if (this.state.selectedGameId === null) {
}

export default createContainer(() => {
Meteor.subscribe('games');

return {
user: Meteor.user(),
games: Games.find().fetch()
Expand Down
3 changes: 2 additions & 1 deletion imports/ui/GameBoard.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React, { Component } from 'react';
import {GamesController} from '../api/controllers/gamesController.js';
import {Game, GameStatuses} from '../api/models/game.js';
import {userMarkGame} from '../api/methods/games.js';

export default class GameBoard extends Component {
handleCellClick(row, col) {
let game = this.props.game;
if (game.currentPlayerIndex() !== game.userIndex(this.props.user)) return;
GamesController.userMarkGame(game._id, this.props.user, row, col);
userMarkGame.call({gameId: game._id, row: row, col: col});
}

handleBackToGameList() {
Expand Down
9 changes: 5 additions & 4 deletions imports/ui/GameList.jsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import React, { Component } from 'react';
import {GamesController} from '../api/controllers/gamesController.js';
import {Game, GameStatuses} from '../api/models/game.js';
import {newGame, userJoinGame, userLeaveGame} from '../api/methods/games.js';

export default class GameList extends Component {
handleNewGame() {
GamesController.newGame(this.props.user);
newGame.call({});
}

handleLeaveGame(gameId) {
GamesController.userLeaveGame(gameId, this.props.user);
handleLeaveGame(gameId) {
userLeaveGame.call({gameId: gameId});
}

handleJoinGame(gameId) {
GamesController.userJoinGame(gameId, this.props.user);
userJoinGame.call({gameId: gameId});
}

handleEnterGame(gameId) {
Expand Down
2 changes: 2 additions & 0 deletions server/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {Meteor} from 'meteor/meteor';
import Games from '../imports/api/collections/games.js'; // import Games collection
import '../imports/api/methods/games.js';
import '../imports/api/server/publications.js';

Meteor.startup(() => {
});

0 comments on commit b5c9a39

Please sign in to comment.