From 92fcd9520d11c8430442783db63e19537410b024 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marko=20Syd=C3=A4nmaa?= <L4072@student.jamk.fi>
Date: Mon, 24 Jun 2019 10:44:11 +0300
Subject: [PATCH] Corrected database and started role controller

---
 src/game/coordinate.entity.ts      | 14 ++++-
 src/game/faction.entity.ts         | 33 ++++--------
 src/game/game.entity.ts            | 20 +++----
 src/mapmarkers/mapmarker.entity.ts |  9 ++--
 src/shared/roles.controller.ts     | 86 ++++++++++++++++++++++++++++++
 src/user/user.entity.ts            |  5 +-
 6 files changed, 121 insertions(+), 46 deletions(-)
 create mode 100644 src/shared/roles.controller.ts

diff --git a/src/game/coordinate.entity.ts b/src/game/coordinate.entity.ts
index 65ad654..e0087ba 100644
--- a/src/game/coordinate.entity.ts
+++ b/src/game/coordinate.entity.ts
@@ -13,6 +13,7 @@ import {
     GameEntity,
   } from './game.entity';
   import { FactionEntity } from './faction.entity';
+import { type } from 'os';
   
   @Entity('MapDrawing')
   export class MapDrawingEntity {
@@ -22,9 +23,20 @@ import {
   
     @Column({ type: 'json', nullable: true }) data: JSON;
   
-    @ManyToOne(type => FactionEntity, faction => faction.mapDrawings)
+    @ManyToOne(type => FactionEntity, faction => faction.factionId)
     faction: FactionEntity;
     @ManyToOne(type => GameEntity, gameEntity => gameEntity.id)
     gameId: GameEntity;
   }
+
+  @Entity('Game_Person_MapDrawing')
+  export class Game_Person_MapDrawingEntity {
+    @PrimaryGeneratedColumn('uuid') GPmapDrawingId: string;
+    @Column({ type: 'timestamp' }) GPCTimeStamp: Timestamp;
+
+    @ManyToOne(type => Game_PersonEntity, game_person => game_person.gamepersonId)
+    game_person: Game_PersonEntity;
+    @ManyToOne(type => MapDrawingEntity, map_drawing => map_drawing.mapDrawingId)
+    map_drawing: MapDrawingEntity;
+  }
   
\ No newline at end of file
diff --git a/src/game/faction.entity.ts b/src/game/faction.entity.ts
index b0a99df..214f487 100644
--- a/src/game/faction.entity.ts
+++ b/src/game/faction.entity.ts
@@ -19,18 +19,8 @@ export class FactionEntity {
   @Column({ type: 'text' }) factionPassword: string;
   @Column({ type: 'float' }) multiplier: number;
 
-  @OneToMany(type => MapDrawingEntity, mapDrawings => mapDrawings.faction)
-  mapDrawings: MapDrawingEntity[];
-  @OneToMany(type => ScoreEntity, scores => scores.faction)
-  scores: ScoreEntity[];
-  @OneToMany(type => PowerUpEntity, powerUps => powerUps.factions)
-  powerUps: Faction_PowerUpEntity[];
-  @OneToMany(type => TaskEntity, tasks => tasks.faction)
-  tasks: TaskEntity[];
-  @OneToMany(type => Game_PersonEntity, game_persons => game_persons.faction)
-  game_persons: Game_PersonEntity[];
-  @ManyToOne(type => GameEntity, game => game.factions)
-  game: GameEntity;
+  @ManyToOne(type => GameEntity, gameEntity => gameEntity.id)
+  gameId: GameEntity;
 }
 
 @Entity('PowerUp')
@@ -40,21 +30,16 @@ export class PowerUpEntity {
   @Column({ type: 'text' }) powerUpDescription: string;
   @Column({ type: 'int' }) amount: number;
   @Column({ type: 'time' }) cooldown: string;
-
-  @OneToMany(type => FactionEntity, factions => factions.powerUps)
-  factions: Faction_PowerUpEntity[];
 }
 
 @Entity('Faction_PowerUp')
 export class Faction_PowerUpEntity {
   @PrimaryGeneratedColumn('uuid') faction_powerUpId: string;
 
-  @ManyToOne(type => FactionEntity, faction => faction.powerUps)
+  @ManyToOne(type => FactionEntity, faction => faction.factionId)
   faction: FactionEntity;
-  @ManyToOne(type => PowerUpEntity, powerUp => powerUp.factions)
+  @ManyToOne(type => PowerUpEntity, powerUp => powerUp.powerUpId)
   powerUp: PowerUpEntity;
-  @OneToMany(type => FP_HistoryEntity, histories => histories.faction_PowerUp)
-  histories: FP_HistoryEntity[];
 }
 
 @Entity('FP_History')
@@ -64,7 +49,7 @@ export class FP_HistoryEntity {
 
   @ManyToOne(
     type => Faction_PowerUpEntity,
-    faction_PowerUp => faction_PowerUp.histories,
+    faction_PowerUp => faction_PowerUp.faction_powerUpId,
   )
   faction_PowerUp: Faction_PowerUpEntity;
 }
@@ -75,7 +60,7 @@ export class ScoreEntity {
   @Column({ type: 'float' }) score: number;
   @Column({ type: 'timestamp' }) scoreTimeStamp: Timestamp;
 
-  @ManyToOne(type => FactionEntity, factionName => factionName.scores)
+  @ManyToOne(type => FactionEntity, factionName => factionName.factionId)
   faction: FactionEntity;
 }
 
@@ -87,8 +72,8 @@ export class TaskEntity {
   @Column({ type: 'text' }) taskWinner: string;
   @Column({ type: 'bool' }) taskIsActive: boolean;
 
-  @ManyToOne(type => FactionEntity, faction => faction.tasks)
+  @ManyToOne(type => FactionEntity, faction => faction.factionId)
   faction: FactionEntity;
-  /*     @ManyToOne(type => PersonEntity, person => person.tasks)
-    person: PersonEntity; */
+  @ManyToOne(type => Game_PersonEntity, person => person.gamepersonId)
+  personId: Game_PersonEntity;
 }
diff --git a/src/game/game.entity.ts b/src/game/game.entity.ts
index 0ef442c..66ec21f 100644
--- a/src/game/game.entity.ts
+++ b/src/game/game.entity.ts
@@ -11,8 +11,8 @@ import {
 
 import { PersonEntity } from '../user/user.entity';
 import { GameGroupEntity } from './group.entity';
-import { FactionEntity } from './faction.entity';
-import { MapDrawingEntity } from './coordinate.entity';
+import { FactionEntity, TaskEntity } from './faction.entity';
+import { MapDrawingEntity, Game_Person_MapDrawingEntity } from './coordinate.entity';
 
 // table that stores all created games
 @Entity('Game')
@@ -24,8 +24,7 @@ export class GameEntity {
   @Column({ type: 'json', nullable: true }) map: JSON;
   @Column('timestamp') startdate: Timestamp;
   @Column('timestamp') enddate: Timestamp;
-  @OneToMany(type => FactionEntity, faction => faction.game)
-  factions: FactionEntity[];
+  
   @OneToMany(type => Game_PersonEntity, game_persons => game_persons.game)
   game_persons: Game_PersonEntity[];
   @OneToMany(type => GameGroupEntity, group => group.game)
@@ -47,11 +46,11 @@ export class GameEntity {
 export class Game_PersonEntity {
   @PrimaryGeneratedColumn('uuid') gamepersonId: string;
   @Column({ type: 'text', nullable: true }) role: string;
-  @ManyToOne(type => FactionEntity, faction => faction.game_persons)
+  @ManyToOne(type => FactionEntity, faction => faction.factionId)
   faction: FactionEntity;
-  @ManyToOne(type => GameEntity, game => game.game_persons)
+  @ManyToOne(type => GameEntity, game => game.id)
   game: GameEntity;
-  @ManyToOne(type => PersonEntity, person => person.game_persons)
+  @ManyToOne(type => PersonEntity, person => person.id)
   person: PersonEntity;
   @OneToOne(type => GameGroupEntity, group => group.leader, {
     onDelete: 'CASCADE',
@@ -75,11 +74,6 @@ export class ObjectivePointEntity {
   coordinate: MapDrawingEntity;
   @ManyToOne(type => GameEntity, game => game.objective_points)
   game: GameEntity;
-  @OneToMany(
-    type => ObjectivePoint_HistoryEntity,
-    op_history => op_history.objective_point,
-  )
-  op_history: Game_PersonEntity[];
 }
 
 @Entity('ObjectivePoint_History')
@@ -90,7 +84,7 @@ export class ObjectivePoint_HistoryEntity {
 
   @ManyToOne(
     type => ObjectivePointEntity,
-    objective_point => objective_point.op_history,
+    objective_point => objective_point.objectivePointId,
   )
   objective_point: ObjectivePointEntity;
 }
diff --git a/src/mapmarkers/mapmarker.entity.ts b/src/mapmarkers/mapmarker.entity.ts
index b83b1d9..237951a 100644
--- a/src/mapmarkers/mapmarker.entity.ts
+++ b/src/mapmarkers/mapmarker.entity.ts
@@ -7,20 +7,21 @@ import {
   } from 'typeorm';
   
   import { PersonEntity } from '../user/user.entity';
+import { GameEntity, Game_PersonEntity } from 'src/game/game.entity';
   
   /*
   Entity: MapMarker 
   - represents data that database contains on mapmarker
   */
   
-  @Entity('MapMarker')
+ /* @Entity('MapMarker')
   export class MapMarkerEntity {
     @PrimaryGeneratedColumn('uuid') id: string;
     @Column({ type: 'text', nullable: true }) latitude: string;
     @Column({ type: 'text', nullable: true }) longitude: string;
     @Column({ type: 'timestamp' }) timestamp: Timestamp;
     @Column({ type: 'json', nullable: true }) features: JSON;
-    @ManyToOne(type => PersonEntity, player => player.markers)
-    player: PersonEntity;
+    @ManyToOne(type => Game_PersonEntity, player => player.markers)
+    player: Game_PersonEntity;
   }
-  
\ No newline at end of file
+  */
\ No newline at end of file
diff --git a/src/shared/roles.controller.ts b/src/shared/roles.controller.ts
new file mode 100644
index 0000000..8af761c
--- /dev/null
+++ b/src/shared/roles.controller.ts
@@ -0,0 +1,86 @@
+const AccessControl = require('accesscontrol');
+
+const grants = {
+    admin: {
+        mapmarker: {
+            "create:any": [],
+            "delete:any": [],
+            "read:any": [],
+            "update:any": []
+        },
+        powerup: {
+            "create:any": [],
+            "delete:any": [],
+            "read:any": [],
+            "update:any": []
+        },
+        faction: {
+            "create:any": [],
+            "delete:any": [],
+            "read:any": [],
+            "update:any": []
+        },
+        players: {
+            "create:any": [],
+            "delete:any": [],
+            "read:any": [],
+            "update:any": []
+        }
+    },
+    faction_leader: {
+        mapmarker: {
+            "create:own": [],
+            "delete:own": [],
+            "read:own": []
+        },
+        powerup: {
+            "read:own": []
+        },
+        faction: {
+            "read:own": [],
+            "update:own": []
+        },
+        players: {
+            "read:own": [],
+            "update:own": []
+        }
+    }
+};
+
+const ac = new AccessControl(grants);
+
+/*const express = require ('express');
+const router express.Router;
+*/
+/*const ac = new AccessControl();
+ac.grant('faction_leader')                    // define new or modify existing role. also takes an array.
+    .createOwn('mapmarker')             // equivalent to .createOwn('video', ['*'])
+    .deleteOwn('mapmarker')
+    .readOwn('mapmarker')
+  .grant('admin')                   // switch to another role without breaking the chain
+    .extend('user')                 // inherit role capabilities. also takes an array
+    .updateAny('mapmarker', ['title'])  // explicitly defined attributes
+    .deleteAny('mapmarker')
+    .readAny('mapmarker');
+
+/*const*//*let permission = ac.can('user').createOwn('mapmarker');
+console.log(permission.granted);    // —> true
+console.log(permission.attributes); // —> ['*'] (all attributes)
+
+permission = ac.can('admin').updateAny('mapmarker');
+console.log(permission.granted);    // —> true
+console.log(permission.attributes); // —> ['title']
+
+/*router.get('/videos/:title', function (req, res, next) {
+    const permission = ac.can(req.user.role).readAny('video');
+    if (permission.granted) {
+        Video.find(req.params.title, function (err, data) {
+            if (err || !data) return res.status(404).end();
+            // filter data by permission attributes and send.
+            res.json(permission.filter(data));
+        });
+    } else {
+        // resource is forbidden for this user/role
+        res.status(403).end();
+    }
+});*/
\ No newline at end of file
diff --git a/src/user/user.entity.ts b/src/user/user.entity.ts
index 6323836..99dbea9 100644
--- a/src/user/user.entity.ts
+++ b/src/user/user.entity.ts
@@ -7,8 +7,6 @@ import {
 } from 'typeorm';
 import * as bcrypt from 'bcryptjs';
 import * as jwt from 'jsonwebtoken';
-import { MapMarkerEntity } from 'src/mapmarkers/mapmarker.entity';
-import {TaskEntity} from '../game/faction.entity'
 import {Game_PersonEntity} from '../game/game.entity'
 
 @Entity('Person')
@@ -16,8 +14,7 @@ export class PersonEntity {
   @PrimaryGeneratedColumn('uuid') id: string;
   @Column({ type: 'text', unique: true }) name: string;
   @Column('text') password: string;
-  @OneToMany(type => MapMarkerEntity, marker => marker.player)
-  markers: MapMarkerEntity[];
+  
   @OneToMany(type => Game_PersonEntity, game_persons => game_persons.person)
   game_persons: Game_PersonEntity[];
 
-- 
GitLab