Fakultas Ilmu Komputer UI

Commit 6dc21553 authored by M. Reza Qorib's avatar M. Reza Qorib
Browse files

[#140656091] #29 add authorization function

parent 65fba637
/* eslint-disable no-multi-comp */
import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, browserHistory, Redirect } from 'react-router';
......@@ -8,12 +9,6 @@ import VacancyPage from './VacancyPage';
import Server from './lib/Server';
import Storage from './lib/Storage';
export const Profile = () => (
<Segment>
<img src="http://semantic-ui.com/images/wireframe/media-paragraph.png" alt="wire-frame" />
</Segment>
);
export default class App extends React.Component {
......@@ -24,31 +19,73 @@ export default class App extends React.Component {
this.handleHome = this.handleHome.bind(this);
}
authorization = WrappedComponent =>
allowedRoles => (
class WithAuthorization extends React.Component {
constructor(props) {
super(props);
this.state = {
user: {
role: WithAuthorization.getRole(),
},
};
}
static getRole() {
if (Server.isLoggedIn()) {
if (Storage.get('user-data').is_staff) {
return 'admin';
} else if (Storage.get('user-data').supervisor) {
return 'supervisor';
} else if (Storage.get('user-data').student) {
return 'student';
} else if (Storage.get('user-data').company) {
return 'company';
}
return 'error';
}
return 'not-logged-in';
}
render() {
const { role } = this.state.user;
if (allowedRoles.includes(role)) {
return <WrappedComponent {...this.props} />;
}
return browserHistory.goBack;
}
});
handleAuth = (nextState, replace) => (
Server.isLoggedIn() || replace({ pathname: '/login' })
);
handleHome= (nextState, replace) => {
if (Server.isLoggedIn()) {
Storage.get('user-data').student ? replace({pathname: '/lowongan'}) : replace({pathname: '/profile'});
Storage.get('user-data').student ? replace({ pathname: '/lowongan' }) : replace({ pathname: '/profile' });
} else {
replace({ pathname: '/login' });
}
};
render = () => (
<Router history={browserHistory}>
<Route path="/login" component={Login} />
<Route component={Dashboard} onEnter={this.handleAuth}>
<Route path="/" component={VacancyPage} />
<Route path="/profile" component={Profile} />
<Route path="/lowongan" component={VacancyPage} />
<Route path="/users" component={Profile} />
</Route>
<Route path="/home" onEnter={this.handleHome} />
<Redirect from="*" to="/home" />
</Router>
);
render() {
const student = this.authorization(['admin', 'student']);
const supervisor = this.authorization(['admin', 'supervisor']);
const company = this.authorization(['admin', 'company']);
const commonUser = this.authorization(['admin', 'student', 'company']);
return (
<Router history={browserHistory}>
<Route path="/login" component={Login}/>
<Route component={Dashboard} onEnter={this.handleAuth}>
<Route path="/lowongan" component={commonUser(VacancyPage)} />
</Route>
<Route path="/home" onEnter={this.handleHome} />
<Redirect from="*" to="/home" />
</Router>
);
}
}
ReactDOM.render(<App />, document.getElementById('react-app'));
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment