From 9212a1c68760c4ef386b161317f4640234da5de3 Mon Sep 17 00:00:00 2001 From: Pekka Helenius Date: Fri, 16 Oct 2020 16:09:56 +0300 Subject: [PATCH] Implement BookStoreWebRestrictions class Signed-off-by: Pekka Helenius --- .../session/BookStoreWebRestrictions.java | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 bookstore/src/main/java/com/fjordtek/bookstore/service/session/BookStoreWebRestrictions.java diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/service/session/BookStoreWebRestrictions.java b/bookstore/src/main/java/com/fjordtek/bookstore/service/session/BookStoreWebRestrictions.java new file mode 100644 index 0000000..7c3bdb0 --- /dev/null +++ b/bookstore/src/main/java/com/fjordtek/bookstore/service/session/BookStoreWebRestrictions.java @@ -0,0 +1,68 @@ +//Pekka Helenius , Fjordtek 2020 + +package com.fjordtek.bookstore.service.session; + +import java.util.Arrays; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Service; + +import com.fjordtek.bookstore.model.book.BookRepository; + +/** + * + * This service class is implemented due to major + * security risks we encounter when publishing this + * application on the Internet without any + * user account restrictions, as this was intended + * and required for application demonstration purposes. + * + * @author Pekka Helenius + * + */ + +@Service +public class BookStoreWebRestrictions { + + @Autowired + private Environment env; + + @Autowired + private BookRepository bookRepository; + + private boolean areRestrictionsEnabled(String profile) { + + if ( Arrays.stream(env.getActiveProfiles()).anyMatch(p -> p.contains(profile)) ) { + if (Boolean.parseBoolean(env.getProperty("security.enabled"))) { + return true; + } + } + return false; + } + + public boolean limitBookMaxCount(String profile) { + + if (areRestrictionsEnabled(profile)) { + + if (env.getProperty("security.book.count.max") != null) { + + try { + int bookCountMax = Integer.parseInt(env.getProperty("security.book.count.max")); + + if (bookRepository.count() == bookCountMax) { + return true; + } + + } catch (NumberFormatException e) { + // TODO logging + } + + } + + } + + return false; + } + +} \ No newline at end of file