Added new quizz button and role
This commit is contained in:
parent
225b5df774
commit
08262f3dfd
@ -4,7 +4,7 @@ import org.springframework.security.core.GrantedAuthority;
|
|||||||
|
|
||||||
public enum Privilege implements GrantedAuthority {
|
public enum Privilege implements GrantedAuthority {
|
||||||
|
|
||||||
LIST_USERS,ADD_USERS,LIST_QUIZZ;
|
LIST_USERS,ADD_USERS,LIST_QUIZZ,CREATE_QUIZZ,VIEW_ALL_FORMS;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getAuthority() {
|
public String getAuthority() {
|
||||||
|
|||||||
@ -12,6 +12,8 @@ public interface QuizzManager {
|
|||||||
public JsonNode answer(User user, long quizzId,JsonNode data);
|
public JsonNode answer(User user, long quizzId,JsonNode data);
|
||||||
public JsonNode next(User user, long quizzId);
|
public JsonNode next(User user, long quizzId);
|
||||||
|
|
||||||
|
public Quizz newQuizz(User user);
|
||||||
|
|
||||||
public boolean canAccessQuizz(User user, long quizzId);
|
public boolean canAccessQuizz(User user, long quizzId);
|
||||||
public List<Quizz> editableQuizz(User user);
|
public List<Quizz> editableQuizz(User user);
|
||||||
public List<Quizz> answerableQuizz(User user);
|
public List<Quizz> answerableQuizz(User user);
|
||||||
|
|||||||
@ -65,6 +65,8 @@ public class QuizzManagerImpl implements QuizzManager {
|
|||||||
Optional<Quizz> oquizz = qRepository.findById(quizzId);
|
Optional<Quizz> oquizz = qRepository.findById(quizzId);
|
||||||
if(!oquizz.isPresent())
|
if(!oquizz.isPresent())
|
||||||
return errorNode("Could not find the quizz with id "+quizzId);
|
return errorNode("Could not find the quizz with id "+quizzId);
|
||||||
|
if(!oquizz.get().isComplete())
|
||||||
|
return errorNode("Quizz is not complete");
|
||||||
Quizz quizz = oquizz.get();
|
Quizz quizz = oquizz.get();
|
||||||
QuizzForm qf = qfRepository.findByUserAndQuizz(user, quizz);
|
QuizzForm qf = qfRepository.findByUserAndQuizz(user, quizz);
|
||||||
if(qf == null)
|
if(qf == null)
|
||||||
@ -124,6 +126,8 @@ public class QuizzManagerImpl implements QuizzManager {
|
|||||||
Optional<Quizz> oquizz = qRepository.findById(quizzId);
|
Optional<Quizz> oquizz = qRepository.findById(quizzId);
|
||||||
if(!oquizz.isPresent())
|
if(!oquizz.isPresent())
|
||||||
return errorNode("Could not find quizz with id "+quizzId);
|
return errorNode("Could not find quizz with id "+quizzId);
|
||||||
|
if(!oquizz.get().isComplete())
|
||||||
|
return errorNode("Quizz is not complete");
|
||||||
Quizz quizz = oquizz.get();
|
Quizz quizz = oquizz.get();
|
||||||
QuizzForm qf = qfRepository.findByUserAndQuizz(user, quizz);
|
QuizzForm qf = qfRepository.findByUserAndQuizz(user, quizz);
|
||||||
if(qf == null){
|
if(qf == null){
|
||||||
@ -165,7 +169,7 @@ public class QuizzManagerImpl implements QuizzManager {
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
public QuizzForm newQuizzForm(User user, Quizz quizz) {
|
private QuizzForm newQuizzForm(User user, Quizz quizz) {
|
||||||
QuizzForm qf = new QuizzForm();
|
QuizzForm qf = new QuizzForm();
|
||||||
qf.setUser(user);
|
qf.setUser(user);
|
||||||
qf.setQuizz(quizz);
|
qf.setQuizz(quizz);
|
||||||
@ -176,7 +180,16 @@ public class QuizzManagerImpl implements QuizzManager {
|
|||||||
return qf;
|
return qf;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final JsonNode errorNode(String err){
|
@Override
|
||||||
|
public Quizz newQuizz(User user) {
|
||||||
|
Quizz q = new Quizz();
|
||||||
|
q.setName("Super questions de "+user.getName()+" ("+Integer.toHexString((int)(Math.random()*0xFFFFFFF))+")");
|
||||||
|
q.setOwner(user);
|
||||||
|
q = qRepository.save(q);
|
||||||
|
return q;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final JsonNode errorNode(String err){
|
||||||
ObjectNode out = JsonNodeFactory.instance.objectNode();
|
ObjectNode out = JsonNodeFactory.instance.objectNode();
|
||||||
out.set("success", JsonNodeFactory.instance.booleanNode(false));
|
out.set("success", JsonNodeFactory.instance.booleanNode(false));
|
||||||
out.set("message", JsonNodeFactory.instance.textNode(err));
|
out.set("message", JsonNodeFactory.instance.textNode(err));
|
||||||
|
|||||||
@ -41,18 +41,6 @@ public class AuthController {
|
|||||||
return "login";
|
return "login";
|
||||||
}
|
}
|
||||||
|
|
||||||
public User getLoggedInUser() {
|
|
||||||
if(SecurityContextHolder.getContext().getAuthentication().getPrincipal()
|
|
||||||
instanceof org.springframework.security.core.userdetails.User){
|
|
||||||
org.springframework.security.core.userdetails.User user
|
|
||||||
= (org.springframework.security.core.userdetails.User)
|
|
||||||
SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
|
||||||
return userService.findUserByName(user.getUsername());
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/")
|
@GetMapping("/")
|
||||||
public String index(Model model) {
|
public String index(Model model) {
|
||||||
|
|
||||||
|
|||||||
@ -11,10 +11,12 @@ import org.slf4j.LoggerFactory;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.security.access.annotation.Secured;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
|
import com.bernard.misael.model.Privilege;
|
||||||
import com.bernard.misael.model.Quizz;
|
import com.bernard.misael.model.Quizz;
|
||||||
import com.bernard.misael.model.User;
|
import com.bernard.misael.model.User;
|
||||||
import com.bernard.misael.questions.QTypes;
|
import com.bernard.misael.questions.QTypes;
|
||||||
@ -57,6 +59,7 @@ public class QuestionsController {
|
|||||||
model.addAttribute("answerableQuizz",qm.answerableQuizz(u));
|
model.addAttribute("answerableQuizz",qm.answerableQuizz(u));
|
||||||
model.addAttribute("editableQuizz",qm.editableQuizz(u));
|
model.addAttribute("editableQuizz",qm.editableQuizz(u));
|
||||||
}
|
}
|
||||||
|
|
||||||
return "quizz.html";
|
return "quizz.html";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,6 +102,18 @@ public class QuestionsController {
|
|||||||
return new ResponseEntity<>(out, HttpStatus.OK);
|
return new ResponseEntity<>(out, HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/new-quizz")
|
||||||
|
@Secured("CREATE_QUIZZ")
|
||||||
|
public Object newQuizz(Principal p, Model m) {
|
||||||
|
if (p==null)
|
||||||
|
return "redirect:/login?restricted";
|
||||||
|
User u = ur.findByName(p.getName());
|
||||||
|
if (u==null)
|
||||||
|
return new ResponseEntity<>(HttpStatus.FORBIDDEN);
|
||||||
|
Quizz q = qm.newQuizz(u);
|
||||||
|
|
||||||
|
return "redirect:/questions/quizz-edit/"+Long.toString(q.getId());
|
||||||
|
}
|
||||||
|
|
||||||
Logger logger = LoggerFactory.getLogger(QuestionsController.class);
|
Logger logger = LoggerFactory.getLogger(QuestionsController.class);
|
||||||
@GetMapping("/quizz-edit/{q}")
|
@GetMapping("/quizz-edit/{q}")
|
||||||
@ -106,7 +121,6 @@ public class QuestionsController {
|
|||||||
if (p==null)
|
if (p==null)
|
||||||
return "redirect:/login?restricted";
|
return "redirect:/login?restricted";
|
||||||
User u = ur.findByName(p.getName());
|
User u = ur.findByName(p.getName());
|
||||||
logger.info("An INFO Message");
|
|
||||||
if (u==null || !qm.canEditQuizz(u, quizzId))
|
if (u==null || !qm.canEditQuizz(u, quizzId))
|
||||||
return new ResponseEntity<>(HttpStatus.FORBIDDEN);
|
return new ResponseEntity<>(HttpStatus.FORBIDDEN);
|
||||||
m.addAttribute("quizzId", quizzId);
|
m.addAttribute("quizzId", quizzId);
|
||||||
|
|||||||
8
src/main/resources/db/migration/V5__new_privileges.sql
Normal file
8
src/main/resources/db/migration/V5__new_privileges.sql
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
alter table role_privileges drop constraint role_privileges_privileges_check;
|
||||||
|
alter table role_privileges add constraint role_privileges_privileges_check
|
||||||
|
check (privileges in ('LIST_USERS','ADD_USERS','LIST_QUIZZ','CREATE_QUIZZ','VIEW_ALL_FORMS'));
|
||||||
|
|
||||||
|
insert into role_privileges VALUES
|
||||||
|
((select id from roles where "name" = 'ADMIN'),'CREATE_QUIZZ');
|
||||||
|
insert into role_privileges VALUES
|
||||||
|
((select id from roles where "name" = 'ADMIN'),'VIEW_ALL_FORMS');
|
||||||
@ -17,6 +17,7 @@
|
|||||||
<li th:if="${#lists.isEmpty(editableQuizz)}">Aucun quizz de disponible malheureusement :(</li>
|
<li th:if="${#lists.isEmpty(editableQuizz)}">Aucun quizz de disponible malheureusement :(</li>
|
||||||
<li th:each="q : ${editableQuizz}"><a th:href="@{/questions/quizz-edit/{id}(id=${q.id})}">Quizz <span th:text="${q.name}"/></a></li>
|
<li th:each="q : ${editableQuizz}"><a th:href="@{/questions/quizz-edit/{id}(id=${q.id})}">Quizz <span th:text="${q.name}"/></a></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
<a sec:authorize="hasAuthority('CREATE_QUIZZ')" th:href="@{/questions/new-quizz}">Nouveau Quizz</a>
|
||||||
</main>
|
</main>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
Loading…
x
Reference in New Issue
Block a user