Skip to content

Commit 9c8c6fa

Browse files
committed
Fix Auth for add lesson
1 parent 932070b commit 9c8c6fa

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

playshogi-library-database/src/main/java/com/playshogi/library/database/LessonRepository.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,30 @@ public boolean isLessonAuthor(final int lessonId, final int userId) {
815815
return false;
816816
}
817817

818+
public boolean isCampaignAuthor(final int campaignId, final int userId) {
819+
Connection connection = dbConnection.getConnection();
820+
821+
try (PreparedStatement preparedStatement = connection.prepareStatement(CHECK_CAMPAIGN_AUTHOR)) {
822+
823+
preparedStatement.setInt(1, campaignId);
824+
preparedStatement.setInt(2, userId);
825+
826+
try (ResultSet resultSet = preparedStatement.executeQuery()) {
827+
if (resultSet.next()) {
828+
return true;
829+
}
830+
}
831+
832+
} catch (SQLException e) {
833+
LOGGER.log(Level.SEVERE,
834+
"Error checking authorship for campaign " + campaignId + " by user " + userId, e);
835+
return false;
836+
}
837+
838+
LOGGER.log(Level.INFO, "User {0} is NOT the author of campaign {1}.", new Object[]{userId, campaignId});
839+
return false;
840+
}
841+
818842
private static final String FIND_CHAPTERS_BY_LESSON_ID =
819843
"SELECT c.chapter_id, c.lesson_id, c.kifu_id, c.type, c.title, c.chapter_number, c.orientation, c.hidden," +
820844
" k.usf " +

playshogi-website/playshogi-website-server/src/main/java/com/playshogi/website/gwt/server/services/LessonServiceImpl.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,12 @@ public void addLessonToCampaign(final String sessionId, final String campaignId,
105105
if (loginResult == null || !loginResult.isLoggedIn()) {
106106
throw new IllegalStateException("Only logged in users can add a lesson in campaign");
107107
}
108-
//TODO: auth
108+
109+
// Authorization Check: Check if the current user is the author of the campaign
110+
if (!lessonRepository.isCampaignAuthor(Integer.parseInt(campaignId), loginResult.getUserId())) {
111+
throw new IllegalStateException("User " + loginResult.getUserId() + " is not authorized to modify campaign "
112+
+ campaignId);
113+
}
109114

110115
if (!lessonRepository.addLessonToCampaign(new PersistentCampaignLesson(Integer.parseInt(campaignId),
111116
Integer.parseInt(lessonId), x, y, false,
@@ -133,7 +138,13 @@ public void updateCampaignNode(final String sessionId, final String campaignId,
133138

134139
LoginResult loginResult = authenticator.checkSession(sessionId);
135140
if (loginResult == null || !loginResult.isLoggedIn()) {
136-
throw new IllegalStateException("Only logged in users can delete a lesson in campaign");
141+
throw new IllegalStateException("Only logged in users can update a lesson in campaign");
142+
}
143+
144+
// Authorization Check: Check if the current user is the author of the campaign
145+
if (!lessonRepository.isCampaignAuthor(Integer.parseInt(campaignId), loginResult.getUserId())) {
146+
throw new IllegalStateException("User " + loginResult.getUserId() + " is not authorized to modify campaign "
147+
+ campaignId);
137148
}
138149

139150
if (!lessonRepository.updateCampaignLesson(getPersistentCampaignLesson(Integer.parseInt(campaignId), node))) {
@@ -146,7 +157,19 @@ public void updateCampaignNode(final String sessionId, final String campaignId,
146157

147158
public void setPrerequisites(final String sessionId, final String campaignId, final String lessonId,
148159
List<String> prereqs) {
149-
//TODO auth
160+
LOGGER.log(Level.INFO, "setPrerequisites: " + campaignId);
161+
162+
LoginResult loginResult = authenticator.checkSession(sessionId);
163+
if (loginResult == null || !loginResult.isLoggedIn()) {
164+
throw new IllegalStateException("Only logged in users can update a lesson in campaign");
165+
}
166+
167+
// Authorization Check: Check if the current user is the author of the campaign
168+
if (!lessonRepository.isCampaignAuthor(Integer.parseInt(campaignId), loginResult.getUserId())) {
169+
throw new IllegalStateException("User " + loginResult.getUserId() + " is not authorized to modify campaign "
170+
+ campaignId);
171+
}
172+
150173
lessonRepository.updateLessonPrerequisites(Integer.parseInt(campaignId), Integer.parseInt(lessonId),
151174
prereqs.stream().map(Integer::parseInt).collect(Collectors.toList()));
152175
}

0 commit comments

Comments
 (0)