forked from Archives/Athou_commafeed
case insensitive login (fixes #16)
This commit is contained in:
@@ -1,17 +1,16 @@
|
|||||||
package com.commafeed.backend.dao;
|
package com.commafeed.backend.dao;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.ejb.Stateless;
|
import javax.ejb.Stateless;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
import javax.persistence.NoResultException;
|
||||||
|
import javax.persistence.TypedQuery;
|
||||||
|
|
||||||
import com.commafeed.backend.model.User;
|
import com.commafeed.backend.model.User;
|
||||||
import com.commafeed.backend.model.UserRole;
|
import com.commafeed.backend.model.UserRole;
|
||||||
import com.commafeed.backend.model.UserRole.Role;
|
import com.commafeed.backend.model.UserRole.Role;
|
||||||
import com.commafeed.backend.security.PasswordEncryptionService;
|
import com.commafeed.backend.security.PasswordEncryptionService;
|
||||||
import com.commafeed.frontend.utils.ModelFactory.MF;
|
|
||||||
import com.google.common.collect.Iterables;
|
|
||||||
|
|
||||||
@Stateless
|
@Stateless
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
@@ -20,9 +19,21 @@ public class UserService extends GenericDAO<User> {
|
|||||||
@Inject
|
@Inject
|
||||||
PasswordEncryptionService encryptionService;
|
PasswordEncryptionService encryptionService;
|
||||||
|
|
||||||
|
private User findByName(String name) {
|
||||||
|
TypedQuery<User> query = em.createNamedQuery("User.byName", User.class);
|
||||||
|
query.setParameter("name", name.toLowerCase());
|
||||||
|
|
||||||
|
User user = null;
|
||||||
|
try {
|
||||||
|
user = query.getSingleResult();
|
||||||
|
} catch (NoResultException e) {
|
||||||
|
user = null;
|
||||||
|
}
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
public User login(String name, String password) {
|
public User login(String name, String password) {
|
||||||
List<User> users = findByField(MF.i(MF.p(User.class).getName()), name);
|
User user = findByName(name);
|
||||||
User user = Iterables.getFirst(users, null);
|
|
||||||
if (user != null && !user.isDisabled()) {
|
if (user != null && !user.isDisabled()) {
|
||||||
boolean authenticated = encryptionService.authenticate(password,
|
boolean authenticated = encryptionService.authenticate(password,
|
||||||
user.getPassword(), user.getSalt());
|
user.getPassword(), user.getSalt());
|
||||||
@@ -35,8 +46,7 @@ public class UserService extends GenericDAO<User> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public User register(String name, String password, Collection<Role> roles) {
|
public User register(String name, String password, Collection<Role> roles) {
|
||||||
List<User> users = findByField(MF.i(proxy().getName()), name);
|
if (findByName(name) != null) {
|
||||||
if (!users.isEmpty()) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
User user = new User();
|
User user = new User();
|
||||||
|
|||||||
@@ -4,6 +4,10 @@
|
|||||||
xsi:schemaLocation="
|
xsi:schemaLocation="
|
||||||
http://java.sun.com/xml/ns/persistence/orm
|
http://java.sun.com/xml/ns/persistence/orm
|
||||||
http://java.sun.com/xml/ns/persistence/orm_2_0.xsd">
|
http://java.sun.com/xml/ns/persistence/orm_2_0.xsd">
|
||||||
|
|
||||||
|
<named-query name="User.byName">
|
||||||
|
<query>select u FROM User u WHERE lower(u.name)=:name</query>
|
||||||
|
</named-query>
|
||||||
|
|
||||||
<named-query name="Entry.byGuids">
|
<named-query name="Entry.byGuids">
|
||||||
<query>select e from FeedEntry e where e.guid in (:guids) order by e.updated desc</query>
|
<query>select e from FeedEntry e where e.guid in (:guids) order by e.updated desc</query>
|
||||||
|
|||||||
Reference in New Issue
Block a user