RBAC React Component, displays/hides components based on role
[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 default class SkyquakeRBAC extends React.Component {
25     constructor(props, context) {
26         super(props);
27     }
28     render() {
29       const User = this.context.userProfile.data;
30       let HTML = null;
31       // If user object has platform property then it has been populated by the back end.
32       if(User) {
33         const PlatformRole = User.platform.role;
34         const isPlatformSuper = PlatformRole[PLATFORM.SUPER];
35         const isPlatformAdmin = PlatformRole[PLATFORM.ADMIN];
36         const isPlatformOper = PlatformRole[PLATFORM.OPER];
37         const hasRoleAccess =  checkForRoleAccess(User.project[this.props.project], PlatformRole, this.props.allow)//false//(this.props.roles.indexOf(userProfile.projectRole) > -1)
38         if (isPlatformSuper) {
39           HTML = this.props.children;
40         } else {
41           if (hasRoleAccess) {
42             HTML = this.props.children;
43           }
44         }
45       }
46       return (<div className={this.props.className} style={this.props.style}>{HTML}</div>)
47     }
48 }
49 SkyquakeRBAC.defaultProps = {
50   allow: []
51 }
52 SkyquakeRBAC.contextTypes = {
53   userProfile: React.PropTypes.object
54 }
55
56 function checkForRoleAccess(project, PlatformRole, allow) {
57     if (allow.indexOf('*') > -1) return true;
58     for (let i = 0; i<allow.length; i++) {
59       if((project && project.role[allow[i]] )|| PlatformRole[allow[i]]) {
60         return true
61       }
62     }
63     return false;
64   }
65
66
67
68 // export default function(Component) {
69 //   class SkyquakeRBAC extends React.Component {
70 //     constructor(props, context) {
71 //         super(props);
72 //             }
73 //     render(props) {
74 //       console.log(this.context.userProfile)
75 //       const User = this.context.userProfile.data;
76 //       // If user object has platform property then it has been populated by the back end.
77 //       if(User) {
78 //         const PlatformRole = User.platform.role;
79 //         const HTML = <Component {...this.props} router={this.router} actions={this.actions} flux={this.context.flux}/>;
80 //         const isPlatformSuper = PlatformRole[PLATFORM.SUPER];
81 //         const isPlatformAdmin = PlatformRole[PLATFORM.ADMIN];
82 //         const isPlatformOper = PlatformRole[PLATFORM.OPER];
83 //         const hasRoleAccess =  false//(this.props.roles.indexOf(userProfile.projectRole) > -1)
84 //         if (isPlatformSuper || isPlatformOper || isPlatformAdmin) {
85 //           return HTML
86 //         } else {
87 //           if (hasRoleAccess) {
88 //             return HTML
89 //           } else {
90 //             return null;
91 //           }
92 //         }
93 //       }
94 //       else {
95 //         return null;
96
97 //       }
98 //     }
99 //   }
100 //   SkyquakeRBAC.defaultProps = {
101
102 //   }
103 //   SkyquakeRBAC.contextTypes = {
104 //     userProfile: React.PropTypes.object,
105 //     allowedRoles: []
106 //   };
107 //   return SkyquakeRBAC;
108 // }