11package ru .javawebinar .topjava .service ;
22
3+ import org .springframework .beans .factory .annotation .Autowired ;
34import org .springframework .cache .annotation .CacheEvict ;
45import org .springframework .cache .annotation .Cacheable ;
56import org .springframework .context .annotation .Scope ;
67import org .springframework .context .annotation .ScopedProxyMode ;
8+ import org .springframework .core .env .Environment ;
79import org .springframework .security .core .userdetails .UserDetailsService ;
810import org .springframework .security .core .userdetails .UsernameNotFoundException ;
911import org .springframework .security .crypto .password .PasswordEncoder ;
1012import org .springframework .stereotype .Service ;
1113import org .springframework .transaction .annotation .Transactional ;
1214import org .springframework .util .Assert ;
1315import ru .javawebinar .topjava .AuthorizedUser ;
16+ import ru .javawebinar .topjava .Profiles ;
17+ import ru .javawebinar .topjava .model .AbstractBaseEntity ;
1418import ru .javawebinar .topjava .model .User ;
1519import ru .javawebinar .topjava .repository .UserRepository ;
1620import ru .javawebinar .topjava .to .UserTo ;
1721import ru .javawebinar .topjava .util .UsersUtil ;
22+ import ru .javawebinar .topjava .util .exception .UpdateRestrictionException ;
1823
1924import java .util .List ;
2025
@@ -28,6 +33,14 @@ public class UserService implements UserDetailsService {
2833 private final UserRepository repository ;
2934 private final PasswordEncoder passwordEncoder ;
3035
36+ private boolean modificationRestriction ;
37+
38+ @ Autowired
39+ @ SuppressWarnings ("deprecation" )
40+ public void setEnvironment (Environment environment ) {
41+ modificationRestriction = environment .acceptsProfiles (Profiles .VDS );
42+ }
43+
3144 public UserService (UserRepository repository , PasswordEncoder passwordEncoder ) {
3245 this .repository = repository ;
3346 this .passwordEncoder = passwordEncoder ;
@@ -41,6 +54,7 @@ public User create(User user) {
4154
4255 @ CacheEvict (value = "users" , allEntries = true )
4356 public void delete (int id ) {
57+ checkModificationAllowed (id );
4458 checkNotFound (repository .delete (id ), id );
4559 }
4660
@@ -62,20 +76,23 @@ public List<User> getAll() {
6276 public void update (User user ) {
6377 Assert .notNull (user , "user must not be null" );
6478// checkNotFound : check works only for JDBC, disabled
79+ checkModificationAllowed (user .id ());
6580 prepareAndSave (user );
6681 }
6782
6883
6984 @ CacheEvict (value = "users" , allEntries = true )
7085 @ Transactional
7186 public void update (UserTo userTo ) {
87+ checkModificationAllowed (userTo .id ());
7288 User user = get (userTo .id ());
7389 prepareAndSave (UsersUtil .updateFromTo (user , userTo ));
7490 }
7591
7692 @ CacheEvict (value = "users" , allEntries = true )
7793 @ Transactional
7894 public void enable (int id , boolean enabled ) {
95+ checkModificationAllowed (id );
7996 User user = get (id );
8097 user .setEnabled (enabled );
8198 repository .save (user ); // !! need only for JDBC implementation
@@ -97,4 +114,10 @@ private User prepareAndSave(User user) {
97114 public User getWithMeals (int id ) {
98115 return checkNotFound (repository .getWithMeals (id ), id );
99116 }
117+
118+ protected void checkModificationAllowed (int id ) {
119+ if (modificationRestriction && id < AbstractBaseEntity .START_SEQ + 2 ) {
120+ throw new UpdateRestrictionException ();
121+ }
122+ }
100123}
0 commit comments