// Path: modules/cloud-portal-server/src/main/java/de/papke/cloud/portal/model/CredentialsModel.java
// public class CredentialsModel {
// 	
// 	private List<Credentials> credentialsList;
// 
// 	public List<Credentials> getCredentialsList() {
// 		return credentialsList;
// 	}
// 
// 	public void setCredentialsList(List<Credentials> credentialsList) {
// 		this.credentialsList = credentialsList;
// 	}
// }
// 
// Path: modules/cloud-portal-server/src/main/java/de/papke/cloud/portal/service/CredentialsService.java
// @Service
// public class CredentialsService {
// 	
// 	@Autowired
// 	private EncryptionService encryptionService;
// 	
// 	@Autowired
// 	private SessionUserService sessionUserService;
// 	
// 	@Autowired
// 	private CredentialsDao credentialsDao;
// 	
// 	public List<Credentials> getCredentialList(String provider) {
// 		
// 		List<Credentials> credentialsList = credentialsDao.findByProvider(provider);
// 		
// 		for (Credentials credentials : credentialsList) {
// 			Map<String, String> secretMap = credentials.getSecretMap();
// 			Map<String, String> decryptedSecretMap = decryptSecretMap(secretMap);
// 			credentials.setSecretMap(decryptedSecretMap);
// 		}
// 		
// 		return credentialsList;
// 	}
// 	
// 	public Credentials getCredentials(String provider) {
// 		return getCredentials(sessionUserService.getUser(), provider);
// 	}
// 	
// 	public Credentials getCredentials(User user, String provider) {
// 		
// 		if (user != null && user.getGroups() != null) {
// 			
// 			for (String group : user.getGroups()) {
// 				
// 				Credentials credentials = credentialsDao.findByGroupAndProvider(group, provider);
// 				
// 				if (credentials != null) {
// 					
// 					Map<String, String> secretMap = credentials.getSecretMap();
// 					Map<String, String> decryptedSecretMap = decryptSecretMap(secretMap);
// 					credentials.setSecretMap(decryptedSecretMap);
// 					
// 					return credentials;
// 				}
// 			}
// 		}
// 		
// 		return null;
// 	}
// 	
// 	public Credentials create(String group, String provider, Map<String, String> secretMap) {
// 		
// 		Map<String, String> encryptedSecretMap = encryptSecretMap(secretMap);
// 		Credentials credentials = new Credentials(group, provider, encryptedSecretMap);
// 		
// 		return credentialsDao.save(credentials);
// 	}
// 	
// 	public void delete(String id) {
// 		credentialsDao.delete(id);
// 	}
// 	
// 	private Map<String, String> encryptSecretMap(Map<String, String> secretMap) {
// 		
// 		Map<String, String> encryptedSecretMap = new HashMap<>();
// 		
// 		if (secretMap != null) {
// 			for (Entry<String, String> entry : secretMap.entrySet()) {
// 				String key = entry.getKey();
// 				String value = entry.getValue();
// 				encryptedSecretMap.put(key, encryptionService.encrypt(value));
// 			}
// 		}
// 		
// 		return encryptedSecretMap;
// 	}
// 	
// 	private Map<String, String> decryptSecretMap(Map<String, String> secretMap) {
// 		
// 		Map<String, String> decryptedSecretMap = new HashMap<>();
// 		
// 		if (secretMap != null) {
// 			for (Entry<String, String> entry : secretMap.entrySet()) {
// 				String key = entry.getKey();
// 				String value = entry.getValue();				
// 				decryptedSecretMap.put(key, encryptionService.decrypt(value));
// 			}
// 		}
// 		
// 		return decryptedSecretMap;
// 	}
// }
// 
// Path: modules/cloud-portal-server/src/main/java/de/papke/cloud/portal/service/SessionUserService.java
// @Service
// public class SessionUserService {
// 	
// 	private static final String SESSION_ATTRIBUTE_USER = "user";
// 	private static final User DUMMY_USER = new User("anonymous", "Anonymous", "User", "dummy@dummy.com", new ArrayList<>(), false);
// 	
// 	@Autowired
// 	private HttpSession session;
// 
// 	public User getUser() {
// 		
// 		User user = (User) session.getAttribute(SESSION_ATTRIBUTE_USER);
// 		
// 		if (user == null) {
// 			user = DUMMY_USER; 
// 		}
// 		
// 		return user;
// 	}
// 	
// 	public boolean isAdmin() {
// 		return getUser().isAdmin();
// 	}
// 	
// 	public boolean isAllowed() {
// 		return isAdmin();
// 	}
// 
// 	public boolean isAllowed(ProvisionLog provisionLog) {
// 		return isAdmin() || getUser().getGroups().contains(provisionLog.getGroup());
// 	}
// 	
// 	public void setUser(User user) {
// 		session.setAttribute(SESSION_ATTRIBUTE_USER, user);
// 	}
// }


import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import de.papke.cloud.portal.model.CredentialsModel;
import de.papke.cloud.portal.service.CredentialsService;
import de.papke.cloud.portal.service.SessionUserService;
package de.papke.cloud.portal.controller;




@Controller
public class CredentialsController extends ApplicationController {

	private static final String PREFIX = "/credentials";
	private static final String MODEL_VAR_NAME = "credentials";
	private static final String LIST_PATH_PREFIX = PREFIX + "/list/form";
	private static final String LIST_VIEW_PREFIX = "credentials-list-form-";
	private static final String ATTRIBUTE_GROUP = "group";

	@Autowired
	private CredentialsService credentialsService;

	@Autowired
// Path: modules/cloud-portal-server/src/main/java/de/papke/cloud/portal/model/CredentialsModel.java
// public class CredentialsModel {
// 	
// 	private List<Credentials> credentialsList;
// 
// 	public List<Credentials> getCredentialsList() {
// 		return credentialsList;
// 	}
// 
// 	public void setCredentialsList(List<Credentials> credentialsList) {
// 		this.credentialsList = credentialsList;
// 	}
// }
// 
// Path: modules/cloud-portal-server/src/main/java/de/papke/cloud/portal/service/CredentialsService.java
// @Service
// public class CredentialsService {
// 	
// 	@Autowired
// 	private EncryptionService encryptionService;
// 	
// 	@Autowired
// 	private SessionUserService sessionUserService;
// 	
// 	@Autowired
// 	private CredentialsDao credentialsDao;
// 	
// 	public List<Credentials> getCredentialList(String provider) {
// 		
// 		List<Credentials> credentialsList = credentialsDao.findByProvider(provider);
// 		
// 		for (Credentials credentials : credentialsList) {
// 			Map<String, String> secretMap = credentials.getSecretMap();
// 			Map<String, String> decryptedSecretMap = decryptSecretMap(secretMap);
// 			credentials.setSecretMap(decryptedSecretMap);
// 		}
// 		
// 		return credentialsList;
// 	}
// 	
// 	public Credentials getCredentials(String provider) {
// 		return getCredentials(sessionUserService.getUser(), provider);
// 	}
// 	
// 	public Credentials getCredentials(User user, String provider) {
// 		
// 		if (user != null && user.getGroups() != null) {
// 			
// 			for (String group : user.getGroups()) {
// 				
// 				Credentials credentials = credentialsDao.findByGroupAndProvider(group, provider);
// 				
// 				if (credentials != null) {
// 					
// 					Map<String, String> secretMap = credentials.getSecretMap();
// 					Map<String, String> decryptedSecretMap = decryptSecretMap(secretMap);
// 					credentials.setSecretMap(decryptedSecretMap);
// 					
// 					return credentials;
// 				}
// 			}
// 		}
// 		
// 		return null;
// 	}
// 	
// 	public Credentials create(String group, String provider, Map<String, String> secretMap) {
// 		
// 		Map<String, String> encryptedSecretMap = encryptSecretMap(secretMap);
// 		Credentials credentials = new Credentials(group, provider, encryptedSecretMap);
// 		
// 		return credentialsDao.save(credentials);
// 	}
// 	
// 	public void delete(String id) {
// 		credentialsDao.delete(id);
// 	}
// 	
// 	private Map<String, String> encryptSecretMap(Map<String, String> secretMap) {
// 		
// 		Map<String, String> encryptedSecretMap = new HashMap<>();
// 		
// 		if (secretMap != null) {
// 			for (Entry<String, String> entry : secretMap.entrySet()) {
// 				String key = entry.getKey();
// 				String value = entry.getValue();
// 				encryptedSecretMap.put(key, encryptionService.encrypt(value));
// 			}
// 		}
// 		
// 		return encryptedSecretMap;
// 	}
// 	
// 	private Map<String, String> decryptSecretMap(Map<String, String> secretMap) {
// 		
// 		Map<String, String> decryptedSecretMap = new HashMap<>();
// 		
// 		if (secretMap != null) {
// 			for (Entry<String, String> entry : secretMap.entrySet()) {
// 				String key = entry.getKey();
// 				String value = entry.getValue();				
// 				decryptedSecretMap.put(key, encryptionService.decrypt(value));
// 			}
// 		}
// 		
// 		return decryptedSecretMap;
// 	}
// }
// 
// Path: modules/cloud-portal-server/src/main/java/de/papke/cloud/portal/service/SessionUserService.java
// @Service
// public class SessionUserService {
// 	
// 	private static final String SESSION_ATTRIBUTE_USER = "user";
// 	private static final User DUMMY_USER = new User("anonymous", "Anonymous", "User", "dummy@dummy.com", new ArrayList<>(), false);
// 	
// 	@Autowired
// 	private HttpSession session;
// 
// 	public User getUser() {
// 		
// 		User user = (User) session.getAttribute(SESSION_ATTRIBUTE_USER);
// 		
// 		if (user == null) {
// 			user = DUMMY_USER; 
// 		}
// 		
// 		return user;
// 	}
// 	
// 	public boolean isAdmin() {
// 		return getUser().isAdmin();
// 	}
// 	
// 	public boolean isAllowed() {
// 		return isAdmin();
// 	}
// 
// 	public boolean isAllowed(ProvisionLog provisionLog) {
// 		return isAdmin() || getUser().getGroups().contains(provisionLog.getGroup());
// 	}
// 	
// 	public void setUser(User user) {
// 		session.setAttribute(SESSION_ATTRIBUTE_USER, user);
// 	}
// }


// Path: modules/cloud-portal-server/src/main/java/de/papke/cloud/portal/controller/CredentialsController.java
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import de.papke.cloud.portal.model.CredentialsModel;
import de.papke.cloud.portal.service.CredentialsService;
import de.papke.cloud.portal.service.SessionUserService;

package de.papke.cloud.portal.controller;




@Controller
public class CredentialsController extends ApplicationController {

	private static final String PREFIX = "/credentials";
	private static final String MODEL_VAR_NAME = "credentials";
	private static final String LIST_PATH_PREFIX = PREFIX + "/list/form";
	private static final String LIST_VIEW_PREFIX = "credentials-list-form-";
	private static final String ATTRIBUTE_GROUP = "group";

	@Autowired
	private CredentialsService credentialsService;

	@Autowired
	private SessionUserService sessionUserService;