Skip to content

Commit 335cc7b

Browse files
committed
user edit by admin
1 parent 99f5009 commit 335cc7b

File tree

15 files changed

+53
-63
lines changed

15 files changed

+53
-63
lines changed

app/models/ability.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class Ability
33

44
def initialize(user)
55
user ||= User.new # guest user (not logged in)
6-
if user.admin?
6+
if user.is_admin?
77
can :manage, :all
88
else
99
can :manage, Post

app/models/user.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,9 @@ class User < ActiveRecord::Base
1010

1111
scope :email_contains, -> (value) { where('email ILIKE ?', "%#{value.join}%") }
1212

13-
def admin?
14-
has_role?(:admin)
15-
end
16-
1713
def token_validation_response
1814
self.as_json(except: [
1915
:tokens, :created_at, :updated_at
20-
]).merge("role" => self.roles.first.name)
16+
]).merge(roles: self.roles.map(&:name))
2117
end
2218
end

app/resources/user_resource.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
class UserResource < JSONAPI::Resource
22
extend ModelFilter
3-
attributes :email, :confirmed_at, :created_at
4-
attribute :role
5-
6-
has_many :roles
3+
attributes :email, :confirmed_at, :created_at, :roles
74

85
paginator :paged
96
model_filters :email_contains
107

11-
def role
12-
self._model.roles.first.name
8+
def roles
9+
@model.roles.pluck(:name)
10+
end
11+
12+
def roles=(roles)
13+
@model.roles.destroy_all
14+
roles.map do |role|
15+
@model.add_role role
16+
end
1317
end
1418
end

client/src/api/normalize.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const serializers = {
4545
keyForAttribute: 'camelCase',
4646
attributes: [
4747
'email',
48+
'roles',
4849
],
4950
}),
5051
deserializer: new Deserializer({

client/src/components/App.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class App extends Component {
2222

2323
render() {
2424
const { user } = this.props;
25-
const userIsAdmin = user.role === "admin"
25+
const userIsAdmin = user.roles.includes('admin')
2626

2727
return (
2828
<div>
@@ -51,7 +51,7 @@ export class App extends Component {
5151
</DropdownToggle>
5252
<DropdownMenu right>
5353
<DropdownItem>
54-
<Link to="/profile">Profile</Link>
54+
<DropdownItem>Profile</DropdownItem>
5555
</DropdownItem>
5656
<DropdownItem href onClick={this.logout}> Logout </DropdownItem>
5757
</DropdownMenu>

client/src/components/Profiles/ProfileEdit.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

client/src/components/Profiles/ProfileForm.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

client/src/components/Profiles/index.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

client/src/components/Routes.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ import Dashboard from './Dashboard';
88
import { PostList, PostEdit } from './Posts';
99
import { CategoryList, CategoryEdit } from './Categories';
1010
import { UserList, UserEdit } from './Users';
11-
import { ProfileEdit } from './Profiles';
1211
import { Login } from './Auth';
1312

1413
const UserIsAuthenticated = UserAuthWrapper({ authSelector: getUser });
14+
const UserIsAdmin = UserAuthWrapper({
15+
authSelector: getUser,
16+
predicate: user => user.roles.includes('admin')
17+
});
1518

1619
export class Routes extends PureComponent {
1720
static propTypes = {
@@ -29,9 +32,8 @@ export class Routes extends PureComponent {
2932
<Route path="/posts/new" component={PostEdit}/>
3033
<Route path="/posts/:id" component={PostEdit}/>
3134
<Route path="/categories" component={CategoryList}/>
32-
<Route path="/users" component={UserList}/>
33-
<Route path="/users/:id" component={UserEdit}/>
34-
<Route path="/profile" component={ProfileEdit}/>
35+
<Route path="/users" component={UserIsAdmin(UserList)}/>
36+
<Route path="/users/:id" component={UserIsAdmin(UserEdit)}/>
3537
</Route>
3638
<Route path="/login" component={Login}/>
3739
</Router>

client/src/components/Users/UserEdit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class UserEdit extends Component {
1414
fetchRoles();
1515

1616
if (params.id) {
17-
fetchResource({ id: params.id, include: 'roles' });
17+
fetchResource({ id: params.id });
1818
}
1919
}
2020

0 commit comments

Comments
 (0)