update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b third try
[osm/UI.git] / skyquake / framework / widgets / skyquake_rbac / skyquakeRBAC.jsx
1 /*
2  *
3  *   Copyright 2016 RIFT.IO Inc
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  *
17  */
18
19
20 import React from 'react';
21 import ROLES from 'utils/roleConstants.js';
22 const PLATFORM = ROLES.PLATFORM;
23
24 export function isRBACValid(User, allow, Project){
25   const UserData = User && User.data;
26   if(UserData) {
27       const PlatformRole = UserData.platform.role;
28       const isPlatformSuper = PlatformRole[PLATFORM.SUPER];
29       const isPlatformAdmin = PlatformRole[PLATFORM.ADMIN];
30       const isPlatformOper = PlatformRole[PLATFORM.OPER];
31       const hasRoleAccess =  checkForRoleAccess(UserData.project[Project || User.projectId], PlatformRole, allow)//false//(this.props.roles.indexOf(userProfile.projectRole) > -1)
32       if (isPlatformSuper) {
33         return true;
34       } else {
35         if (hasRoleAccess) {
36           return true;
37         }
38       }
39     }
40   return false;
41 }
42
43 export default class SkyquakeRBAC extends React.Component {
44     constructor(props, context) {
45         super(props);
46     }
47     render() {
48       const User = this.context.userProfile;
49       const UserData = User.data;
50       const Project = this.props.project;
51       let HTML = null;
52       // If user object has platform property then it has been populated by the back end.
53       if(isRBACValid(User, this.props.allow, Project)) {
54         HTML = this.props.children;
55       }
56       return (<div className={this.props.className} style={this.props.style}>{HTML}</div>)
57     }
58 }
59 SkyquakeRBAC.defaultProps = {
60   allow: [],
61   project: false
62 }
63 SkyquakeRBAC.contextTypes = {
64   userProfile: React.PropTypes.object
65 }
66
67 function checkForRoleAccess(project, PlatformRole, allow) {
68     if (allow.indexOf('*') > -1) return true;
69     for (let i = 0; i<allow.length; i++) {
70       if((project && project.role[allow[i]])|| PlatformRole[allow[i]]) {
71         return true
72       }
73     }
74     return false;
75   }
76
77
78
79 // export default function(Component) {
80 //   class SkyquakeRBAC extends React.Component {
81 //     constructor(props, context) {
82 //         super(props);
83 //             }
84 //     render(props) {
85 //       console.log(this.context.userProfile)
86 //       const User = this.context.userProfile.data;
87 //       // If user object has platform property then it has been populated by the back end.
88 //       if(User) {
89 //         const PlatformRole = User.platform.role;
90 //         const HTML = <Component {...this.props} router={this.router} actions={this.actions} flux={this.context.flux}/>;
91 //         const isPlatformSuper = PlatformRole[PLATFORM.SUPER];
92 //         const isPlatformAdmin = PlatformRole[PLATFORM.ADMIN];
93 //         const isPlatformOper = PlatformRole[PLATFORM.OPER];
94 //         const hasRoleAccess =  false//(this.props.roles.indexOf(userProfile.projectRole) > -1)
95 //         if (isPlatformSuper || isPlatformOper || isPlatformAdmin) {
96 //           return HTML
97 //         } else {
98 //           if (hasRoleAccess) {
99 //             return HTML
100 //           } else {
101 //             return null;
102 //           }
103 //         }
104 //       }
105 //       else {
106 //         return null;
107
108 //       }
109 //     }
110 //   }
111 //   SkyquakeRBAC.defaultProps = {
112
113 //   }
114 //   SkyquakeRBAC.contextTypes = {
115 //     userProfile: React.PropTypes.object,
116 //     allowedRoles: []
117 //   };
118 //   return SkyquakeRBAC;
119 // }