Skip to content

Commit

Permalink
SAK-49950 webapi dashboard use nickname in welcome if available (saka…
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianfish authored Jun 20, 2024
1 parent 52be9b3 commit ac11be2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 34 deletions.
4 changes: 4 additions & 0 deletions webapi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.sakaiproject.common</groupId>
<artifactId>sakai-common-api</artifactId>
</dependency>
<!-- third party dependencies -->
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import org.apache.commons.fileupload.FileItem;

import org.sakaiproject.api.common.edu.person.SakaiPerson;
import org.sakaiproject.api.common.edu.person.SakaiPersonManager;
import org.sakaiproject.portal.api.PortalConstants;
import org.sakaiproject.webapi.beans.DashboardRestBean;
import org.sakaiproject.announcement.api.AnnouncementMessage;
Expand Down Expand Up @@ -51,8 +53,9 @@

import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -61,7 +64,6 @@
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -83,29 +85,33 @@ public class DashboardController extends AbstractSakaiApiController implements E
private static final String COURSE_IMAGE = "course_image";
private static final String COURSE_IMAGE_FILE = COURSE_IMAGE + ".png";

@Resource
private AnnouncementService announcementService;
@Autowired
private AnnouncementService announcementService;

@Autowired
private ContentHostingService contentHostingService;

@Resource
private ContentHostingService contentHostingService;
@Autowired
private EntityManager entityManager;

@Resource
private EntityManager entityManager;
@Autowired
private SecurityService securityService;

@Resource
private SecurityService securityService;
@Autowired
@Qualifier("org.sakaiproject.component.api.ServerConfigurationService")
private ServerConfigurationService serverConfigurationService;

@Resource(name = "org.sakaiproject.component.api.ServerConfigurationService")
private ServerConfigurationService serverConfigurationService;
@Autowired
private SiteService siteService;

@Resource
private SiteService siteService;
@Autowired
private UserDirectoryService userDirectoryService;

@Resource
private UserDirectoryService userDirectoryService;
@Autowired
private PreferencesService preferencesService;

@Resource
private PreferencesService preferencesService;
@Autowired
private SakaiPersonManager sakaiPersonManager;

private List<String> courseWidgets = new ArrayList<>();
private List<String> homeWidgets = new ArrayList<>();
Expand Down Expand Up @@ -163,11 +169,11 @@ public void init() {
entityManager.registerEntityProducer(this, REFERENCE_ROOT);
}

@GetMapping(value = "/users/{userId}/dashboard", produces = MediaType.APPLICATION_JSON_VALUE)
@GetMapping(value = "/users/{userId}/dashboard", produces = MediaType.APPLICATION_JSON_VALUE)
public DashboardRestBean getUserDashboard(@PathVariable String userId) throws UserNotDefinedException {

Session session = checkSakaiSession();
String currentUserId = session.getUserId();
Session session = checkSakaiSession();
String currentUserId = session.getUserId();

DashboardRestBean bean = new DashboardRestBean();

Expand All @@ -176,7 +182,12 @@ public DashboardRestBean getUserDashboard(@PathVariable String userId) throws Us
}

try {
bean.setGivenName(userDirectoryService.getUser(currentUserId).getFirstName());
SakaiPerson sakaiPerson = sakaiPersonManager.getSakaiPerson(currentUserId, sakaiPersonManager.getUserMutableType());
if (sakaiPerson != null && StringUtils.isNotBlank(sakaiPerson.getNickname())) {
bean.setGivenName(sakaiPerson.getNickname());
} else {
bean.setGivenName(userDirectoryService.getUser(currentUserId).getFirstName());
}
} catch (UserNotDefinedException unde) {
log.warn("No user found for id {}", currentUserId);
}
Expand Down Expand Up @@ -232,13 +243,13 @@ public DashboardRestBean getUserDashboard(@PathVariable String userId) throws Us
}

return bean;
}
}

@PutMapping(value = "/users/{userId}/dashboard")
@PutMapping(value = "/users/{userId}/dashboard")
public void saveUserDashboard(@PathVariable String userId, @RequestBody DashboardRestBean bean) throws UserNotDefinedException {

String currentUserId = checkSakaiSession().getUserId();
if (!securityService.isSuperUser() && (!StringUtils.isBlank(userId) && !StringUtils.equals(userId, currentUserId))) {
String currentUserId = checkSakaiSession().getUserId();
if (!securityService.isSuperUser() && (!StringUtils.isBlank(userId) && !StringUtils.equals(userId, currentUserId))) {
log.error("You can only update your own user dashboard.");
return;
}
Expand Down Expand Up @@ -266,12 +277,12 @@ public void saveUserDashboard(@PathVariable String userId, @RequestBody Dashboar
if (preference != null) preferencesService.commit(preference);
}
}
}
}

@GetMapping(value = "/sites/{siteId}/dashboard", produces = MediaType.APPLICATION_JSON_VALUE)
@GetMapping(value = "/sites/{siteId}/dashboard", produces = MediaType.APPLICATION_JSON_VALUE)
public DashboardRestBean getSiteDashboard(@PathVariable String siteId) throws UserNotDefinedException {

Session session = checkSakaiSession();
Session session = checkSakaiSession();

DashboardRestBean bean = new DashboardRestBean();

Expand Down Expand Up @@ -312,12 +323,12 @@ public DashboardRestBean getSiteDashboard(@PathVariable String siteId) throws Us
}

return bean;
}
}

@PutMapping(value = "/sites/{siteId}/dashboard")
@PutMapping(value = "/sites/{siteId}/dashboard")
public void saveSiteDashboard(@PathVariable String siteId, @RequestBody DashboardRestBean bean) throws UserNotDefinedException {

Session session = checkSakaiSession();
Session session = checkSakaiSession();

try {
Site site = siteService.getSite(siteId);
Expand All @@ -331,9 +342,9 @@ public void saveSiteDashboard(@PathVariable String siteId, @RequestBody Dashboar
siteService.save(site);
} catch (Exception e) {
}
}
}

@PostMapping(value = "/sites/{siteId}/image", produces = "text/plain")
@PostMapping(value = "/sites/{siteId}/image", produces = "text/plain")
public String saveSiteImage(HttpServletRequest req, @PathVariable String siteId) throws Exception {

try {
Expand Down

0 comments on commit ac11be2

Please sign in to comment.