@@ -99,10 +99,9 @@ trait ChangePassword extends JacksonSupport with AuthenticationSupport {
9999
100100 validateWithMsg(reqBody.getAnyProblem) {
101101 val timestamp : java.sql.Timestamp = ApiTime .nowUTCTimestamp
102+ val isOAuthEnabled = Configuration .getConfig.hasPath(" api.authentication.oauth.provider.user_info.url" )
102103
103104 val action =
104- for {
105- numUsersModified <-
106105 Compiled (UsersTQ .filter(user => (user.organization === organization &&
107106 user.username === username))
108107 .filterIf(identity.isUser && ! identity.isSuperUser)(users => (users.organization ++ " /" ++ users.username) =!= " root/root" )
@@ -116,10 +115,33 @@ trait ChangePassword extends JacksonSupport with AuthenticationSupport {
116115 .update(timestamp,
117116 identity.identifier,
118117 Option (Password .hash(reqBody.newPassword))) // Grab this last second.
119- } yield numUsersModified
118+
119+ val checkExternalUserQuery = Compiled (
120+ UsersTQ .filter(user => user.organization === organization && user.username === username)
121+ .filter(_.identityProvider =!= " Open Horizon" )
122+ .take(1 )
123+ .length
124+ )
125+
126+ val checkUserAndUpdate : DBIOAction [Int , NoStream , Effect .Read with Effect .Write ] =
127+ if (isOAuthEnabled) {
128+ for {
129+ externalUserCount <- checkExternalUserQuery.result
130+
131+ _ <- if (externalUserCount > 0 ) {
132+ DBIO .failed(new MethodNotAllowedException (ExchMsg .translate(" password.disabled.oauth.user" )))
133+ } else {
134+ DBIO .successful(())
135+ }
136+
137+ numUsersModified <- action
138+ } yield numUsersModified
139+ } else {
140+ action
141+ }
120142
121143 complete {
122- db.run(action .transactionally.asTry).map {
144+ db.run(checkUserAndUpdate .transactionally.asTry).map {
123145 case Success (numUsersModified) =>
124146 Future { logger.debug(" POST /orgs/" + organization + " /users/" + username + " /changepw result: " + numUsersModified)
125147 }
@@ -135,6 +157,8 @@ trait ChangePassword extends JacksonSupport with AuthenticationSupport {
135157 }
136158 else
137159 (HttpCode .NOT_FOUND , ApiResponse (ApiRespType .NOT_FOUND , ExchMsg .translate(" user.not.found" , resource)))
160+ case Failure (t : MethodNotAllowedException ) =>
161+ (HttpCode .NOT_ALLOWED , ApiResponse (ApiRespType .METHOD_NOT_ALLOWED , ExchMsg .translate(" password.disabled.oauth.user" )))
138162 case Failure (t : org.postgresql.util.PSQLException ) =>
139163 ExchangePosgtresErrorHandling .ioProblemError(t, ExchMsg .translate(" user.password.not.updated" , resource, t.toString))
140164 case Failure (t) =>
@@ -143,7 +167,7 @@ trait ChangePassword extends JacksonSupport with AuthenticationSupport {
143167 }
144168 }
145169 }
146-
170+
147171 def changePassword (identity : Identity2 ): Route =
148172 path(" orgs" / Segment / " users" / Segment / " changepw" ) {
149173 (organization, username) =>
0 commit comments