Skip to content

Commit

Permalink
Added logic so non-admin users only see their own data
Browse files Browse the repository at this point in the history
  • Loading branch information
robmelfi committed Oct 14, 2018
1 parent ac05397 commit 0c440a2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
13 changes: 12 additions & 1 deletion src/main/java/com/robmelfi/health/service/PointsService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.robmelfi.health.service;

import com.robmelfi.health.domain.Points;
import com.robmelfi.health.domain.User;
import com.robmelfi.health.repository.PointsRepository;
import com.robmelfi.health.repository.UserRepository;
import com.robmelfi.health.repository.search.PointsSearchRepository;
import com.robmelfi.health.security.AuthoritiesConstants;
import com.robmelfi.health.security.SecurityUtils;
import com.robmelfi.health.service.dto.PointsDTO;
import com.robmelfi.health.service.mapper.PointsMapper;
import org.slf4j.Logger;
Expand Down Expand Up @@ -32,10 +36,13 @@ public class PointsService {

private final PointsSearchRepository pointsSearchRepository;

public PointsService(PointsRepository pointsRepository, PointsMapper pointsMapper, PointsSearchRepository pointsSearchRepository) {
private final UserRepository userRepository;

public PointsService(PointsRepository pointsRepository, PointsMapper pointsMapper, PointsSearchRepository pointsSearchRepository, UserRepository userRepository) {
this.pointsRepository = pointsRepository;
this.pointsMapper = pointsMapper;
this.pointsSearchRepository = pointsSearchRepository;
this.userRepository = userRepository;
}

/**
Expand All @@ -48,6 +55,10 @@ public PointsDTO save(PointsDTO pointsDTO) {
log.debug("Request to save Points : {}", pointsDTO);

Points points = pointsMapper.toEntity(pointsDTO);
if (!SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.ADMIN)) {
log.debug("No user passed in, using current user: {}", SecurityUtils.getCurrentUserLogin());
points.setUser(userRepository.findOneByLogin(SecurityUtils.getCurrentUserLogin().get()).get());
}
points = pointsRepository.save(points);
PointsDTO result = pointsMapper.toDto(points);
pointsSearchRepository.save(points);
Expand Down
25 changes: 15 additions & 10 deletions src/main/webapp/app/entities/points/points-update.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { IPoints } from 'app/shared/model/points.model';
// tslint:disable-next-line:no-unused-variable
import { convertDateTimeFromServer } from 'app/shared/util/date-utils';
import { mapIdList } from 'app/shared/util/entity-utils';
import { hasAnyAuthority } from 'app/shared/auth/private-route';
import { AUTHORITIES } from 'app/config/constants';

export interface IPointsUpdateProps extends StateProps, DispatchProps, RouteComponentProps<{ id: string }> {}

Expand Down Expand Up @@ -69,7 +71,7 @@ export class PointsUpdate extends React.Component<IPointsUpdateProps, IPointsUpd
};

render() {
const { pointsEntity, users, loading, updating } = this.props;
const { pointsEntity, users, loading, updating, isAdmin } = this.props;
const { isNew } = this.state;

return (
Expand Down Expand Up @@ -130,18 +132,20 @@ export class PointsUpdate extends React.Component<IPointsUpdateProps, IPointsUpd
}}
/>
</AvGroup>
<AvGroup>
<Label for="user.login">User</Label>
<AvInput id="points-user" type="select" className="form-control" name="userId">
{users
? users.map(otherEntity => (
{isAdmin &&
<AvGroup>
<Label for="user.login">User</Label>
<AvInput id="points-user" type="select" className="form-control" name="userId">
{users
? users.map(otherEntity => (
<option value={otherEntity.id} key={otherEntity.id}>
{otherEntity.login}
</option>
))
: null}
</AvInput>
</AvGroup>
: null}
</AvInput>
</AvGroup>
}
<Button tag={Link} id="cancel-save" to="/entity/points" replace color="info">
<FontAwesomeIcon icon="arrow-left" />&nbsp;
<span className="d-none d-md-inline">Back</span>
Expand All @@ -163,7 +167,8 @@ const mapStateToProps = (storeState: IRootState) => ({
users: storeState.userManagement.users,
pointsEntity: storeState.points.entity,
loading: storeState.points.loading,
updating: storeState.points.updating
updating: storeState.points.updating,
isAdmin: hasAnyAuthority(storeState.authentication.account.authorities, [AUTHORITIES.ADMIN]),
});

const mapDispatchToProps = {
Expand Down

0 comments on commit 0c440a2

Please sign in to comment.