mirror of
https://gitee.com/jmix/cuba.git
synced 2024-12-03 19:57:36 +08:00
UserSettingsService
This commit is contained in:
parent
e278455616
commit
61281e5367
2
cuba.ipr
2
cuba.ipr
@ -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">
|
||||
|
@ -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)));
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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"/>
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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"));
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user