UserSettingsService

This commit is contained in:
Konstantin Krivopustov 2009-03-17 11:07:38 +00:00
parent e278455616
commit 61281e5367
10 changed files with 298 additions and 2 deletions

View File

@ -1072,6 +1072,7 @@
<root url="jar://$PROJECT_DIR$/../lib/common/src/reflections-0.9-src.zip!/" />
<root url="jar://$PROJECT_DIR$/../lib/common/src/commons-io-1.4-src.zip!/" />
<root url="jar://$PROJECT_DIR$/../lib/common/src/perf4j-0.9.10-src.zip!/" />
<root url="intellijad://intellijad" />
</SOURCES>
</library>
<library name="test">
@ -1086,6 +1087,7 @@
<JAVADOC />
<SOURCES>
<root url="jar://$PROJECT_DIR$/../lib/test/src/junit-4.5-src.zip!/" />
<root url="intellijad://intellijad" />
</SOURCES>
</library>
<library name="client">

View File

@ -172,6 +172,23 @@ alter table SEC_CONSTRAINT add constraint SEC_CONSTRAINT_GROUP foreign key (GROU
------------------------------------------------------------------------------------------------------------
create table SEC_USER_SETTING (
ID binary(16),
CREATE_TS timestamp,
CREATED_BY varchar(20),
USER_ID binary(16),
CLIENT_TYPE char(1),
NAME varchar(255),
VALUE text,
primary key (ID)
)^
alter table SEC_USER_SETTING add constraint SEC_USER_SETTING_USER foreign key (USER_ID) references SEC_USER(ID)^
alter table SEC_USER_SETTING add constraint SEC_USER_SETTING_UNIQ unique (USER_ID, NAME, CLIENT_TYPE)^
------------------------------------------------------------------------------------------------------------
create function from_id(uuid binary(16)) returns char(36)
begin
return concat(hex(left(uuid,4)),'-', hex(mid(uuid,5,2)),'-', hex(mid(uuid,7,2)),'-',hex(mid(uuid,9,2)),'-',hex(right(uuid,6)));

View File

@ -123,7 +123,7 @@ create table SEC_USER_ROLE (
primary key (ID)
);
alter table SEC_USER_ROLE add constraint SEC_USER_ROLE_PROFILE foreign key (USER_ID) references SEC_USER(ID);
alter table SEC_USER_ROLE add constraint SEC_USER_ROLE_USER foreign key (USER_ID) references SEC_USER(ID);
alter table SEC_USER_ROLE add constraint SEC_USER_ROLE_ROLE foreign key (ROLE_ID) references SEC_ROLE(ID);
@ -172,6 +172,23 @@ alter table SEC_CONSTRAINT add constraint SEC_CONSTRAINT_GROUP foreign key (GROU
------------------------------------------------------------------------------------------------------------
create table SEC_USER_SETTING (
ID varchar(36),
CREATE_TS timestamp,
CREATED_BY varchar(20),
USER_ID varchar(36),
CLIENT_TYPE char(1),
NAME varchar(255),
VALUE longvarchar,
primary key (ID)
);
alter table SEC_USER_SETTING add constraint SEC_USER_SETTING_USER foreign key (USER_ID) references SEC_USER(ID);
alter table SEC_USER_SETTING add constraint SEC_USER_SETTING_UNIQ unique (USER_ID, NAME, CLIENT_TYPE);
------------------------------------------------------------------------------------------------------------
insert into SEC_GROUP (ID, CREATE_TS, VERSION, NAME, PARENT_ID)
values ('0fa2b1a5-1d68-4d69-9fbd-dff348347f93', current_timestamp, 0, 'Company', null);

View File

@ -15,6 +15,7 @@
<class>com.haulmont.cuba.security.entity.GroupHierarchy</class>
<class>com.haulmont.cuba.security.entity.Permission</class>
<class>com.haulmont.cuba.security.entity.Constraint</class>
<class>com.haulmont.cuba.security.entity.UserSetting</class>
<properties>
<property name="openjpa.ManagedRuntime" value="org.apache.openjpa.ee.JNDIManagedRuntime"/>

View File

@ -30,4 +30,13 @@ public enum ClientType
public String getConfigPath() {
return configPath;
}
public static ClientType fromId(String id) {
if ("W".equals(id))
return WEB;
else if ("D".equals(id))
return DESKTOP;
else
return null;
}
}

View File

@ -0,0 +1,33 @@
/*
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
* Haulmont Technology proprietary and confidential.
* Use is subject to license terms.
* Author: Konstantin Krivopustov
* Created: 16.03.2009 19:13:22
*
* $Id$
*/
package com.haulmont.cuba.security.app;
import com.haulmont.cuba.core.global.ClientType;
import javax.ejb.Local;
@Local
public interface UserSettingService
{
String JNDI_NAME = "cuba/security/UserSettingService";
/** Load settings for the current user and null client type */
String loadSetting(String name);
/** Load settings for the current user */
String loadSetting(ClientType clientType, String name);
/** Save settings for the current user and null client type */
void saveSetting(String name, String value);
/** Save settings for the current user */
void saveSetting(ClientType clientType, String name, String value);
}

View File

@ -0,0 +1,85 @@
/*
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
* Haulmont Technology proprietary and confidential.
* Use is subject to license terms.
* Author: Konstantin Krivopustov
* Created: 17.03.2009 11:36:02
*
* $Id$
*/
package com.haulmont.cuba.security.app;
import com.haulmont.cuba.core.EntityManager;
import com.haulmont.cuba.core.PersistenceProvider;
import com.haulmont.cuba.core.Query;
import com.haulmont.cuba.core.SecurityProvider;
import com.haulmont.cuba.core.global.ClientType;
import com.haulmont.cuba.core.global.View;
import com.haulmont.cuba.core.sys.ServiceInterceptor;
import com.haulmont.cuba.security.entity.User;
import com.haulmont.cuba.security.entity.UserSetting;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.interceptor.Interceptors;
import java.util.List;
@Stateless(name = UserSettingService.JNDI_NAME)
@Interceptors({ServiceInterceptor.class})
public class UserSettingServiceBean implements UserSettingService
{
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public String loadSetting(String name) {
return loadSetting(null, name);
}
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public String loadSetting(ClientType clientType, String name) {
EntityManager em = PersistenceProvider.getEntityManager();
Query q = em.createQuery(
"select s from sec$UserSetting s where s.user.id = ?1 and s.name =?2 and s.clientType = ?3");
q.setParameter(1, SecurityProvider.currentUserSession().getUserId());
q.setParameter(2, name);
q.setParameter(3, clientType == null ? null : clientType.getId());
q.setView(new View(UserSetting.class, false).addProperty("value"));
List<UserSetting> list = q.getResultList();
String value = list.isEmpty() ? null : list.get(0).getValue();
return value;
}
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void saveSetting(String name, String value) {
saveSetting(null, name, value);
}
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void saveSetting(ClientType clientType, String name, String value) {
EntityManager em = PersistenceProvider.getEntityManager();
Query q = em.createQuery(
"select s from sec$UserSetting s where s.user.id = ?1 and s.name =?2 and s.clientType = ?3");
q.setParameter(1, SecurityProvider.currentUserSession().getUserId());
q.setParameter(2, name);
q.setParameter(3, clientType == null ? null : clientType.getId());
q.setView(new View(UserSetting.class, false).addProperty("value"));
List<UserSetting> list = q.getResultList();
if (list.isEmpty()) {
UserSetting us = new UserSetting();
em.setView(new View(User.class, false));
us.setUser(em.find(User.class, SecurityProvider.currentUserSession().getUserId()));
us.setName(name);
us.setClientType(clientType);
us.setValue(value);
em.persist(us);
} else {
UserSetting us = list.get(0);
us.setValue(value);
}
}
}

View File

@ -0,0 +1,69 @@
/*
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
* Haulmont Technology proprietary and confidential.
* Use is subject to license terms.
* Author: Konstantin Krivopustov
* Created: 16.03.2009 18:52:46
*
* $Id$
*/
package com.haulmont.cuba.security.entity;
import com.haulmont.cuba.core.entity.BaseUuidEntity;
import com.haulmont.cuba.core.entity.Versioned;
import com.haulmont.cuba.core.global.ClientType;
import javax.persistence.*;
@Entity(name = "sec$UserSetting")
@Table(name = "SEC_USER_SETTING")
public class UserSetting extends BaseUuidEntity
{
private static final long serialVersionUID = -4324101071593066529L;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "USER_ID")
private User user;
@Column(name = "CLIENT_TYPE", length = 1)
private String clientType;
@Column(name = "NAME", length = 255)
private String name;
@Column(name = "VALUE")
private String value;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public ClientType getClientType() {
return ClientType.fromId(clientType);
}
public void setClientType(ClientType clientType) {
this.clientType = clientType == null ? null : clientType.getId();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

View File

@ -20,9 +20,11 @@ import java.util.UUID;
public class TestSecurityProvider extends SecurityProvider
{
public static final String USER_ID = "60885987-1b61-4247-94c7-dff348347f93";
protected UserSession __currentUserSession() {
User user = new User();
user.setId(UUID.fromString("60885987-1b61-4247-94c7-dff348347f93"));
user.setId(UUID.fromString(USER_ID));
user.setLogin("test_admin");
user.setName("Test Administrator");
user.setPassword(DigestUtils.md5Hex("test_admin"));

View File

@ -0,0 +1,61 @@
/*
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
* Haulmont Technology proprietary and confidential.
* Use is subject to license terms.
* Author: Konstantin Krivopustov
* Created: 17.03.2009 12:42:08
*
* $Id$
*/
package com.haulmont.cuba.security;
import com.haulmont.cuba.core.*;
import com.haulmont.cuba.core.sys.TestSecurityProvider;
import com.haulmont.cuba.core.global.ClientType;
import com.haulmont.cuba.security.app.UserSettingService;
public class UserSettingServiceTest extends CubaTestCase
{
private UserSettingService uss;
protected void setUp() throws Exception {
super.setUp();
uss = Locator.lookupLocal(UserSettingService.JNDI_NAME);
}
protected void tearDown() throws Exception {
Transaction tx = Locator.createTransaction();
try {
EntityManager em = PersistenceProvider.getEntityManager();
Query q = em.createNativeQuery("delete from SEC_USER_SETTING where USER_ID = ?");
q.setParameter(1, TestSecurityProvider.USER_ID);
q.executeUpdate();
tx.commit();
} finally {
tx.end();
}
super.tearDown();
}
public void test() {
String val = uss.loadSetting(ClientType.WEB, "test-setting");
assertNull(val);
uss.saveSetting(ClientType.WEB, "test-setting", "test-value");
val = uss.loadSetting(ClientType.WEB, "test-setting");
assertEquals("test-value", val);
val = uss.loadSetting("test-setting");
assertNull(val);
uss.saveSetting("test-setting", "test-value-1");
val = uss.loadSetting("test-setting");
assertEquals("test-value-1", val);
val = uss.loadSetting(ClientType.WEB, "test-setting");
assertEquals("test-value", val);
}
}