Skip to content

Commit

Permalink
fix(ZNTA-2387): handle some null safety
Browse files Browse the repository at this point in the history
  • Loading branch information
djansen-redhat committed Apr 24, 2018
1 parent 9b854f9 commit 0b0d9ab
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
Expand Up @@ -47,7 +47,8 @@ public HAccountActivationKey findByAccountIdAndKeyHash(Long accountId, String ke
"from HAccountActivationKey key where key.account.id = :accountId and key.keyHash= :keyHash")
.setLong("accountId", accountId)
.setString("keyHash", keyHash)
.setComment("AccountDAO.getByUsernameAndEmail").uniqueResult();
.setComment("AccountActivationKeyDAO.findByAccountIdAndKeyHash")
.uniqueResult();
}

}
36 changes: 18 additions & 18 deletions server/services/src/main/java/org/zanata/dao/AccountDAO.kt
@@ -1,22 +1,22 @@
/*
* Copyright 2018, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.zanata.dao

Expand Down Expand Up @@ -60,15 +60,15 @@ class AccountDAO : AbstractDAOImpl<HAccount, Long> {
return Optional.ofNullable(getByUsername(username))
}

fun getByEmail(email: String): HAccount {
fun getByEmail(email: String): HAccount? {
return session.createQuery(
"from HAccount acc where acc.person.email = :email") .apply {
setString("email", email)
comment = ("AccountDAO.getByEmail")
}.uniqueResult() as HAccount
}

fun getByUsernameAndEmail(username: String, email: String): HAccount {
fun getByUsernameAndEmail(username: String, email: String): HAccount? {
return session.createQuery(
"from HAccount acc where acc.username = :username " +
"and acc.person.email = :email").apply {
Expand All @@ -77,7 +77,7 @@ class AccountDAO : AbstractDAOImpl<HAccount, Long> {
}.uniqueResult() as HAccount
}

fun getByApiKey(apikey: String): HAccount {
fun getByApiKey(apikey: String): HAccount? {
return session.createCriteria(HAccount::class.java)
.add(Restrictions.eq("apiKey", apikey))
.uniqueResult() as HAccount
Expand Down Expand Up @@ -151,7 +151,7 @@ class AccountDAO : AbstractDAOImpl<HAccount, Long> {
}
}

fun getByCredentialsId(credentialsId: String): HAccount {
fun getByCredentialsId(credentialsId: String): HAccount? {
return session.createQuery(
"select c.account from HCredentials c where c.user = :id")
.apply {
Expand Down
Expand Up @@ -26,6 +26,7 @@
import javax.inject.Inject;

import org.hibernate.Session;
import org.zanata.exception.NoSuchUserException;
import org.zanata.model.AllowedApp;
import org.zanata.model.HAccount;

Expand All @@ -52,6 +53,9 @@ public AllowedAppDAO() {
// TODO support client secret when we support pre-registration of clients
public void persistClientId(String username, String clientId) {
HAccount hAccount = accountDAO.getByUsername(username);
if (hAccount == null) {
throw new NoSuchUserException("Attempted persist with unknown user");
}
AllowedApp allowedApp = new AllowedApp(hAccount, clientId);
hAccount.getAllowedApps().add(allowedApp);

Expand Down
Expand Up @@ -10,6 +10,7 @@
import javax.inject.Inject;
import javax.inject.Named;
import org.zanata.dao.AccountDAO;
import org.zanata.model.HAccount;
import org.zanata.model.HPerson;
import org.zanata.security.ZanataIdentity;
import org.zanata.service.GravatarService;
Expand Down Expand Up @@ -58,10 +59,11 @@ public GetTranslatorListResult execute(GetTranslatorList action,
for (Map.Entry<EditorClientId, PersonSessionDetails> entry : result
.entrySet()) {
PersonId personId = entry.getValue().getPerson().getId();

HPerson person =
accountDAO.getByUsername(personId.toString()).getPerson();

HAccount hAccount = accountDAO.getByUsername(personId.toString());
if (hAccount == null) {
continue;
}
HPerson person = hAccount.getPerson();
Person translator =
new Person(personId, person.getName(),
gravatarServiceImpl.getUserImageUrl(16,
Expand Down

0 comments on commit 0b0d9ab

Please sign in to comment.