Projekt

Obecné

Profil

« Předchozí | Další » 

Revize fab2885e

Přidáno uživatelem Michal Linha před téměř 5 roky(ů)

re #7978 added functionality of only showing assemblies with correct role (same as user), added functionality of showing only logged in users's configurations in "Rychle k"

Zobrazit rozdíly:

src/main/java/vldc/aswi/configuration/AppConfig.java
81 81

  
82 82
	@Override
83 83
	protected void configure(HttpSecurity http) throws Exception {
84
		// TODO: 04.05.2020 Error pages
84 85
		http
85 86
		.authorizeRequests()
86 87
			.mvcMatchers("/login").permitAll()
src/main/java/vldc/aswi/dao/AssemblyRepository.java
3 3
import org.springframework.data.repository.CrudRepository;
4 4
import org.springframework.stereotype.Repository;
5 5
import vldc.aswi.domain.Assembly;
6
import vldc.aswi.domain.Role;
6 7

  
7 8
import java.util.List;
8 9

  
......
27 28
    Assembly findFirst1ByOrderByAssemblyOrder();
28 29

  
29 30
    /**
30
     * Find assemblies order by order
31
     * Find assemblies, order by order
31 32
     * @return ordered assemblies
32 33
     */
33 34
    List<Assembly> getByOrderByAssemblyOrderAsc();
35

  
36
    /**
37
     * Find assemblies with role, order by order
38
     * @return ordered assemblies
39
     */
40
    List<Assembly> getByRolesContainingOrderByAssemblyOrderAsc(Role role);
34 41
}
src/main/java/vldc/aswi/dao/ConfigurationRepository.java
2 2

  
3 3
import org.springframework.data.repository.CrudRepository;
4 4
import org.springframework.stereotype.Repository;
5
import vldc.aswi.domain.Assembly;
6 5
import vldc.aswi.domain.Configuration;
6
import vldc.aswi.domain.User;
7 7

  
8 8
import java.util.List;
9 9

  
......
26 26
     * @return List of configurations.
27 27
     */
28 28
    List<Configuration> getByAssemblyId(Long assemblyId);
29

  
30
    /**
31
     * Get list of configurations by user.
32
     * @param user - user.
33
     * @return List of configurations.
34
     */
35
    List<Configuration> getByUserEquals(User user);
29 36
}
src/main/java/vldc/aswi/service/AssemblyManager.java
56 56
     * @return True if delete was successful, otherwise false.
57 57
     */
58 58
    boolean deleteAssembly(Long id);
59

  
60
    /**
61
     * Get all Assemblies from database with role, ordered.
62
     * @return List of assemblies.
63
     */
64
    List<Assembly> getAssembliesWithRoleOrdered(Role role);
59 65
}
src/main/java/vldc/aswi/service/AssemblyManagerImpl.java
77 77
        return this.assemblyRepository.getByOrderByAssemblyOrderAsc();
78 78
    }
79 79

  
80
    /**
81
     * Get all Assemblies from database with role, ordered.
82
     * @return List of assemblies.
83
     */
84
    @Override
85
    public List<Assembly> getAssembliesWithRoleOrdered(Role role) {
86
        return this.assemblyRepository.getByRolesContainingOrderByAssemblyOrderAsc(role);
87
    }
88

  
80 89
    /**
81 90
     * Get assembly by id.
82 91
     * @param id - ID of assembly.
src/main/java/vldc/aswi/service/ConfigurationManagerImpl.java
5 5
import org.springframework.context.event.ContextRefreshedEvent;
6 6
import org.springframework.context.event.EventListener;
7 7
import org.springframework.core.annotation.Order;
8
import org.springframework.security.authentication.AnonymousAuthenticationToken;
9
import org.springframework.security.core.Authentication;
10
import org.springframework.security.core.context.SecurityContextHolder;
8 11
import org.springframework.stereotype.Service;
9 12
import vldc.aswi.dao.*;
10 13
import vldc.aswi.dao.parameter.ParameterInConfigurationRepository;
11
import vldc.aswi.dao.parameter.ParameterTypeRepository;
12
import vldc.aswi.domain.Assembly;
13
import vldc.aswi.domain.Configuration;
14
import vldc.aswi.domain.Function;
14
import vldc.aswi.domain.*;
15 15
import vldc.aswi.domain.parameter.ParameterInConfiguration;
16 16
import vldc.aswi.service.parameter.ParameterInConfigurationManager;
17 17
import vldc.aswi.service.parameter.ParameterTypeManager;
18
import vldc.aswi.utils.AuthControl;
18 19

  
19 20
import javax.transaction.Transactional;
20 21
import java.util.ArrayList;
21
import java.util.LinkedList;
22 22
import java.util.List;
23 23

  
24 24
@Service
......
83 83
     */
84 84
    @Override
85 85
    public List<Configuration> getConfigurations() {
86
        List<Configuration> retVal = new LinkedList<>();
87
        this.configurationRepository.findAll().forEach(retVal::add);
88
        return retVal;
86
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
87
        String username;
88
        if (!(authentication instanceof AnonymousAuthenticationToken)) {
89
            username = authentication.getName();
90
        }
91
        else {
92
            // TODO: 04.05.2020 error message, user not authenticated
93
            return null;
94
        }
95
        User user = userRepository.findByUsername(username);
96

  
97
        return configurationRepository.getByUserEquals(user);
89 98
    }
90 99

  
91 100
    /**
......
106 115
     */
107 116
    @Override
108 117
    public Configuration saveConfiguration(Configuration newConfiguration, String id) {
109
        if(id.equals("")) {
118
        if (id.equals("")) {
110 119
            return addConfiguration(newConfiguration);
111 120
        }
112 121
        else {
......
158 167
     * @return saved configuration
159 168
     */
160 169
    private Configuration addConfiguration(Configuration newConfiguration) {
170
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
171
        String username = AuthControl.getUserName();
172
        if (username == null) {
173
            // TODO: 04.05.2020 error message, user not authenticated
174
        }
161 175
        Configuration configuration = new Configuration();
162 176
        Assembly assembly = assemblyRepository.getById(newConfiguration.getAssembly().getId());
163 177
        configuration.setAssembly(assembly);
164 178
        configuration.setName(newConfiguration.getName());
165 179
        configuration.setTableName(newConfiguration.getTableName());
166
        configuration.setUser(userRepository.getById((long) 1));
180
        configuration.setUser(userRepository.findByUsername(username));
167 181
        Configuration savedConfiguration = configurationRepository.save(configuration);
168 182

  
169 183
        savedConfiguration.setParametersInConfiguration(new ArrayList<>());
src/main/java/vldc/aswi/service/RoleManager.java
20 20
     * @param name Name of role.
21 21
     */
22 22
    void addRole(String name);
23

  
24
    /**
25
     * Get role from database by name.
26
     * @param name name of the role
27
     * @return List of roles.
28
     */
29
    public Role getRole(String name);
23 30
}
src/main/java/vldc/aswi/service/RoleManagerImpl.java
62 62
        this.roleRepository.findAll().forEach(retVal::add);
63 63
        return retVal;
64 64
    }
65

  
66
    /**
67
     * Get role from database by name.
68
     * @param name name of the role
69
     * @return List of roles.
70
     */
71
    @Override
72
    public Role getRole(String name) {
73
        return roleRepository.getByName(name);
74
    }
65 75
}
src/main/java/vldc/aswi/utils/AuthControl.java
1
package vldc.aswi.utils;
2

  
3
import org.springframework.security.authentication.AnonymousAuthenticationToken;
4
import org.springframework.security.core.Authentication;
5
import org.springframework.security.core.GrantedAuthority;
6
import org.springframework.security.core.context.SecurityContextHolder;
7

  
8
import java.util.Set;
9
import java.util.stream.Collectors;
10

  
11
/**
12
 * Classed used for getting names and roles of logged in users
13
 */
14
public class AuthControl {
15

  
16
    /**
17
     * Gets the name of current user
18
     * @return name of current user
19
     */
20
    public static String getRoleName() {
21
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
22
        Set<String> roles;
23
        if (!(authentication instanceof AnonymousAuthenticationToken)) {
24
            roles = authentication.getAuthorities().stream()
25
                    .map(GrantedAuthority::getAuthority).collect(Collectors.toSet());
26
            return roles.iterator().next().replace("ROLE_", "");
27
        } else {
28
            // TODO: 04.05.2020 error message, user not authenticated
29
            return null;
30
        }
31
    }
32

  
33
    /**
34
     * Gets the role name of the current user
35
     * @return role name of the current user
36
     */
37
    public static String getUserName() {
38
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
39
        if (!(authentication instanceof AnonymousAuthenticationToken)) {
40
            return authentication.getName();
41
        } else {
42
            // TODO: 04.05.2020 error message, user not authenticated
43
            return null;
44
        }
45
    }
46
}
src/main/java/vldc/aswi/web/controller/AssemblyController.java
1 1
package vldc.aswi.web.controller;
2 2

  
3 3
import org.springframework.beans.factory.annotation.Autowired;
4
import org.springframework.security.authentication.AnonymousAuthenticationToken;
5
import org.springframework.security.core.Authentication;
6
import org.springframework.security.core.GrantedAuthority;
7
import org.springframework.security.core.context.SecurityContextHolder;
4 8
import org.springframework.stereotype.Controller;
5 9
import org.springframework.ui.ModelMap;
6 10
import org.springframework.validation.BindingResult;
......
12 16
import org.springframework.web.bind.annotation.RequestParam;
13 17
import org.springframework.web.servlet.ModelAndView;
14 18
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
15
import vldc.aswi.domain.Assembly;
16
import vldc.aswi.domain.Configuration;
17
import vldc.aswi.domain.Location;
18
import vldc.aswi.domain.Operator;
19
import vldc.aswi.domain.*;
19 20
import vldc.aswi.domain.parameter.Parameter;
20 21
import vldc.aswi.domain.parameter.ParameterInConfiguration;
21 22
import vldc.aswi.service.*;
22 23
import vldc.aswi.service.parameter.ParameterManager;
23 24
import vldc.aswi.service.parameter.ParameterTypeManager;
25
import vldc.aswi.utils.AuthControl;
24 26
import vldc.aswi.validators.AssemblyValidator;
25 27
import vldc.aswi.utils.Utils;
26 28

  
27 29
import javax.validation.Valid;
28 30
import java.util.ArrayList;
29 31
import java.util.Comparator;
32
import java.util.Set;
33
import java.util.stream.Collectors;
30 34

  
31 35
/**
32 36
 * Controller for assemblies and configurations
......
104 108

  
105 109
        Assembly assembly = this.assemblyManager.getAssemblyById(Long.parseLong(id));
106 110

  
111
        // TODO: 04.05.2020 error page when id doesn't exist
112

  
113
        String roleName = AuthControl.getRoleName();
114

  
115
        if (roleName == null) {
116
            // TODO: 04.05.2020 error message, user not authenticated
117
        }
118

  
119
        Role role = roleManager.getRole(roleName);
120

  
121
        if (!assembly.getRoles().contains(role)) {
122
            // TODO: 04.05.2020 Error page, wrong role
123
            return new ModelAndView("redirect:/");
124
        }
125

  
107 126
        Configuration configuration = new Configuration();
108 127

  
109 128
        configuration.setAssembly(assembly);
src/main/java/vldc/aswi/web/controller/ConfigurationController.java
1 1
package vldc.aswi.web.controller;
2 2

  
3 3
import org.springframework.beans.factory.annotation.Autowired;
4
import org.springframework.security.authentication.AnonymousAuthenticationToken;
5
import org.springframework.security.core.Authentication;
6
import org.springframework.security.core.context.SecurityContextHolder;
4 7
import org.springframework.stereotype.Controller;
5 8
import org.springframework.ui.ModelMap;
6 9
import org.springframework.validation.BindingResult;
......
14 17
import vldc.aswi.domain.Operator;
15 18
import vldc.aswi.domain.parameter.ParameterInConfiguration;
16 19
import vldc.aswi.service.ConfigurationManager;
20
import vldc.aswi.utils.AuthControl;
17 21
import vldc.aswi.utils.Utils;
18 22

  
19 23
import javax.validation.Valid;
......
44 48

  
45 49
        Configuration configuration = configurationManager.getConfigurationById(Long.parseLong(id));
46 50

  
51
        // TODO: 04.05.2020 error page when id doesn't exist
52

  
53
        String userName = AuthControl.getUserName();
54

  
55
        if (userName == null) {
56
            // TODO: 04.05.2020 error message, user not authenticated
57
        }
58
        else if (!userName.equals(configuration.getUser().getUsername())) {
59
            // TODO: 04.05.2020 error page wrong user
60
            return new ModelAndView("redirect:/");
61
        }
62

  
47 63
        List<ParameterInConfiguration> parametersInConfiguration = new ArrayList<>(configuration.getParametersInConfiguration());
48 64
        configuration.setParametersInConfiguration(parametersInConfiguration);
49 65

  
......
69 85
        ModelAndView modelAndView = new ModelAndView();
70 86

  
71 87
        if (bindingResult.hasErrors()) {
88
            // TODO: 04.05.2020 Error message
72 89
            modelAndView.setViewName("redirect:/");
73 90

  
74 91
            return modelAndView;
src/main/java/vldc/aswi/web/controller/IndexController.java
6 6
import org.springframework.web.bind.annotation.*;
7 7
import org.springframework.web.servlet.ModelAndView;
8 8
import vldc.aswi.domain.Assembly;
9
import vldc.aswi.domain.Role;
9 10
import vldc.aswi.domain.parameter.Parameter;
10 11
import vldc.aswi.service.AssemblyManager;
11 12
import vldc.aswi.service.ConfigurationManager;
13
import vldc.aswi.service.RoleManager;
12 14
import vldc.aswi.service.SqlQueryManager;
15
import vldc.aswi.utils.AuthControl;
13 16

  
14 17
import javax.validation.Valid;
15 18
import java.util.ArrayList;
......
44 47
	@Autowired
45 48
	private ConfigurationManager configurationManager;
46 49

  
50
	/**
51
	 * Autowired role manager
52
	 */
53
	@Autowired
54
	private RoleManager roleManager;
55

  
47 56
	/**
48 57
	 * Shows index page
49 58
	 * @return modelAndView with index page
......
54 63

  
55 64
		ModelMap modelMap = modelAndView.getModelMap();
56 65

  
57
		List<Assembly> assemblies = assemblyManager.getAssembliesOrdered();
66
		String roleName = AuthControl.getRoleName();
67

  
68
		if (roleName == null) {
69
			// TODO: 04.05.2020 error message, user not authenticated
70
		}
71

  
72
		Role role = roleManager.getRole(roleName);
58 73

  
74
		List<Assembly> assemblies = assemblyManager.getAssembliesWithRoleOrdered(role);
59 75

  
60 76
		modelMap.addAttribute("assemblies", assemblies);
61 77
		modelMap.addAttribute("attributes", createAttributesString(assemblies));

Také k dispozici: Unified diff