Skip to content

Commit b5c9a39

Browse files
author
hiukim
committed
part 6 - security
1 parent c861f4b commit b5c9a39

File tree

8 files changed

+69
-9
lines changed

8 files changed

+69
-9
lines changed

.meteor/packages

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ standard-minifier-js@1.1.8 # JS minifier run for production mode
1717
es5-shim@4.6.13 # ECMAScript 5 compatibility for older browsers.
1818
ecmascript@0.5.7 # Enable ECMAScript2015+ syntax in app code
1919

20-
autopublish@1.0.7 # Publish all data to the clients (for prototyping)
21-
insecure@1.0.7 # Allow all DB writes from clients (for prototyping)
2220
react-meteor-data
2321
accounts-password
22+
aldeed:simple-schema
23+
mdg:validated-method

.meteor/versions

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
accounts-base@1.2.9
22
accounts-password@1.2.13
3+
aldeed:simple-schema@1.5.3
34
allow-deny@1.0.5
4-
autopublish@1.0.7
55
autoupdate@1.2.11
66
babel-compiler@6.9.0
77
babel-runtime@0.1.10
@@ -34,12 +34,13 @@ html-tools@1.0.10
3434
htmljs@1.0.10
3535
http@1.1.8
3636
id-map@1.0.8
37-
insecure@1.0.7
3837
jquery@1.11.9
3938
launch-screen@1.0.12
4039
livedata@1.0.18
4140
localstorage@1.0.11
4241
logging@1.1.14
42+
mdg:validated-method@1.1.0
43+
mdg:validation-error@0.2.0
4344
meteor@1.2.16
4445
meteor-base@1.0.4
4546
minifier-css@1.2.13

imports/api/methods/games.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {GamesController} from "../controllers/gamesController.js";
2+
3+
export const newGame = new ValidatedMethod({
4+
name: 'games.newGame',
5+
validate: new SimpleSchema({}).validator(),
6+
run({}) {
7+
GamesController.newGame(Meteor.user());
8+
}
9+
});
10+
11+
export const userJoinGame = new ValidatedMethod({
12+
name: 'games.userJoinGame',
13+
validate: new SimpleSchema({
14+
gameId: {type: String}
15+
}).validator(),
16+
run({gameId}) {
17+
GamesController.userJoinGame(gameId, Meteor.user());
18+
}
19+
});
20+
21+
export const userLeaveGame = new ValidatedMethod({
22+
name: 'games.userLeaveGame',
23+
validate: new SimpleSchema({
24+
gameId: {type: String}
25+
}).validator(),
26+
run({gameId}) {
27+
GamesController.userLeaveGame(gameId, Meteor.user());
28+
}
29+
});
30+
31+
export const userMarkGame = new ValidatedMethod({
32+
name: 'games.userMarkGame',
33+
validate: new SimpleSchema({
34+
gameId: {type: String},
35+
row: {type: Number},
36+
col: {type: Number}
37+
}).validator(),
38+
run({gameId, row, col}) {
39+
GamesController.userMarkGame(gameId, Meteor.user(), row, col);
40+
}
41+
});

imports/api/server/publications.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {GameStatuses} from '../models/game.js';
2+
import Games from '../collections/games.js';
3+
4+
Meteor.publish('games', function() {
5+
// access control: only for loggined-in users
6+
if (this.userId) { // this.userId is the id of the currently loggined in user
7+
// filtering: only games with WAITING and STARTED statuses
8+
return Games.find({status: {$in: [GameStatuses.WAITING, GameStatuses.STARTED]}});
9+
} else {
10+
return null;
11+
}
12+
});

imports/ui/App.jsx

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ if (this.state.selectedGameId === null) {
5656
}
5757

5858
export default createContainer(() => {
59+
Meteor.subscribe('games');
60+
5961
return {
6062
user: Meteor.user(),
6163
games: Games.find().fetch()

imports/ui/GameBoard.jsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import React, { Component } from 'react';
22
import {GamesController} from '../api/controllers/gamesController.js';
33
import {Game, GameStatuses} from '../api/models/game.js';
4+
import {userMarkGame} from '../api/methods/games.js';
45

56
export default class GameBoard extends Component {
67
handleCellClick(row, col) {
78
let game = this.props.game;
89
if (game.currentPlayerIndex() !== game.userIndex(this.props.user)) return;
9-
GamesController.userMarkGame(game._id, this.props.user, row, col);
10+
userMarkGame.call({gameId: game._id, row: row, col: col});
1011
}
1112

1213
handleBackToGameList() {

imports/ui/GameList.jsx

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import React, { Component } from 'react';
22
import {GamesController} from '../api/controllers/gamesController.js';
33
import {Game, GameStatuses} from '../api/models/game.js';
4+
import {newGame, userJoinGame, userLeaveGame} from '../api/methods/games.js';
45

56
export default class GameList extends Component {
67
handleNewGame() {
7-
GamesController.newGame(this.props.user);
8+
newGame.call({});
89
}
910

10-
handleLeaveGame(gameId) {
11-
GamesController.userLeaveGame(gameId, this.props.user);
11+
handleLeaveGame(gameId) {
12+
userLeaveGame.call({gameId: gameId});
1213
}
1314

1415
handleJoinGame(gameId) {
15-
GamesController.userJoinGame(gameId, this.props.user);
16+
userJoinGame.call({gameId: gameId});
1617
}
1718

1819
handleEnterGame(gameId) {

server/main.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {Meteor} from 'meteor/meteor';
22
import Games from '../imports/api/collections/games.js'; // import Games collection
3+
import '../imports/api/methods/games.js';
4+
import '../imports/api/server/publications.js';
35

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

0 commit comments

Comments
 (0)