Skip to content

Commit 3e72a5b

Browse files
author
Zhang Wenhao
committed
<feature>[header]: add AccountVO.source
Resolves: ZSV-12269 DBImpact Change-Id: I7a6c61667a6874657376736b70767a68767a7862
1 parent 24e9696 commit 3e72a5b

15 files changed

Lines changed: 204 additions & 37 deletions

File tree

conf/db/zsv/V5.1.0__schema.sql

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
1-
CREATE TABLE IF NOT EXISTS `zstack`.`TpmKeyBackupVO` (
2-
`uuid` char(32) NOT NULL UNIQUE,
3-
`lastOpDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
4-
`createDate` timestamp NOT NULL DEFAULT '1999-12-31 23:59:59',
5-
PRIMARY KEY (`uuid`)
6-
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
7-
8-
DELETE FROM `EncryptedResourceKeyRefVO`
9-
WHERE `resourceUuid` NOT IN (SELECT `uuid` FROM `ResourceVO`);
10-
ALTER TABLE `EncryptedResourceKeyRefVO`
11-
ADD CONSTRAINT `fkEncryptedResourceKeyRefResourceVO` FOREIGN KEY (`resourceUuid`) REFERENCES `ResourceVO`(`uuid`)
12-
ON DELETE CASCADE;
1+
CREATE TABLE IF NOT EXISTS `zstack`.`TpmKeyBackupVO` (
2+
`uuid` char(32) NOT NULL UNIQUE,
3+
`lastOpDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
4+
`createDate` timestamp NOT NULL DEFAULT '1999-12-31 23:59:59',
5+
PRIMARY KEY (`uuid`)
6+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
7+
8+
DELETE FROM `EncryptedResourceKeyRefVO`
9+
WHERE `resourceUuid` NOT IN (SELECT `uuid` FROM `ResourceVO`);
10+
ALTER TABLE `EncryptedResourceKeyRefVO`
11+
ADD CONSTRAINT `fkEncryptedResourceKeyRefResourceVO` FOREIGN KEY (`resourceUuid`) REFERENCES `ResourceVO`(`uuid`)
12+
ON DELETE CASCADE;
13+
14+
-- Feature: ZCenter Account | ZSV-12257
15+
16+
ALTER TABLE `zstack`.`AccountVO`
17+
ADD COLUMN `source` varchar(32) NOT NULL DEFAULT 'Local' AFTER `type`;
18+
19+
UPDATE `zstack`.`AccountVO` a
20+
INNER JOIN `zstack`.`AccountThirdPartyAccountSourceRefVO` ref ON ref.accountUuid = a.uuid
21+
INNER JOIN `zstack`.`LdapServerVO` ldap ON ldap.uuid = ref.accountSourceUuid
22+
SET a.`source` = IF(ldap.serverType IN ('OpenLdap', 'WindowsAD'), ldap.serverType, 'WindowsAD');
23+
24+
UPDATE `zstack`.`AccountVO` a
25+
INNER JOIN `zstack`.`AccountThirdPartyAccountSourceRefVO` ref ON ref.accountUuid = a.uuid
26+
INNER JOIN `zstack`.`ThirdPartyAccountSourceVO` src ON src.uuid = ref.accountSourceUuid
27+
SET a.`source` = src.type
28+
WHERE src.type IN ('CAS', 'OAuth2');
29+
30+
UPDATE `zstack`.`AccountVO`
31+
SET `type` = 'Normal'
32+
WHERE `type` = 'ThirdParty';

header/src/main/java/org/zstack/header/identity/AccountInventory.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import java.util.ArrayList;
1111
import java.util.Collection;
1212
import java.util.List;
13-
import java.util.UUID;
1413

1514
@Inventory(mappingVOClass = AccountVO.class)
1615
@ExpandedQueries({
@@ -23,6 +22,7 @@ public class AccountInventory {
2322
private String name;
2423
private String description;
2524
private String type;
25+
private String source;
2626
private String state;
2727
private Timestamp createDate;
2828
private Timestamp lastOpDate;
@@ -33,6 +33,7 @@ public static AccountInventory valueOf(AccountVO vo) {
3333
inv.setName(vo.getName());
3434
inv.setDescription(vo.getDescription());
3535
inv.setType(vo.getType().toString());
36+
inv.setSource(vo.getSource().toString());
3637
inv.setState(vo.getState().toString());
3738
inv.setCreateDate(vo.getCreateDate());
3839
inv.setLastOpDate(vo.getLastOpDate());
@@ -55,6 +56,14 @@ public void setType(String type) {
5556
this.type = type;
5657
}
5758

59+
public String getSource() {
60+
return source;
61+
}
62+
63+
public void setSource(String source) {
64+
this.source = source;
65+
}
66+
5867
public String getDescription() {
5968
return description;
6069
}
@@ -109,6 +118,7 @@ public static AccountInventory __example__() {
109118
account.setName("account1");
110119
account.setDescription("account1-description");
111120
account.setType(AccountType.Normal.toString());
121+
account.setSource(AccountSource.Local.toString());
112122
account.setState(AccountState.Enabled.toString());
113123
account.setCreateDate(new Timestamp(DocUtils.date));
114124
account.setLastOpDate(new Timestamp(DocUtils.date));

header/src/main/java/org/zstack/header/identity/AccountInventoryDoc_zh_cn.groovy

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ doc {
3030
type "String"
3131
since "4.0.0"
3232
}
33+
field {
34+
name "source"
35+
desc "账户来源,创建时确定且不可修改"
36+
type "String"
37+
since "5.1.0"
38+
}
3339
field {
3440
name "state"
3541
desc "账户状态"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.zstack.header.identity;
2+
3+
import org.zstack.header.configuration.PythonClass;
4+
5+
import javax.annotation.Nullable;
6+
7+
/**
8+
* Where an account was originally created. Immutable after creation (ZSV-12257).
9+
*/
10+
@PythonClass
11+
public enum AccountSource {
12+
Local,
13+
OpenLdap,
14+
WindowsAD,
15+
CAS,
16+
OAuth2,
17+
ZCenter;
18+
19+
public static AccountSource fromLdapServerTypeName(@Nullable String serverType) {
20+
if (OpenLdap.name().equals(serverType)) {
21+
return OpenLdap;
22+
}
23+
return WindowsAD;
24+
}
25+
}

header/src/main/java/org/zstack/header/identity/AccountType.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@
66
public enum AccountType {
77
SystemAdmin,
88
Normal,
9+
/**
10+
* @deprecated Use {@link AccountType#Normal} with {@link AccountSource} instead (ZSV-12257).
11+
*/
12+
@Deprecated
913
ThirdParty
1014
}

header/src/main/java/org/zstack/header/identity/AccountVO.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public class AccountVO extends ResourceVO {
3131
@Enumerated(EnumType.STRING)
3232
private AccountType type;
3333

34+
@Column(nullable = false)
35+
@Enumerated(EnumType.STRING)
36+
private AccountSource source = AccountSource.Local;
37+
3438
@Column
3539
@Enumerated(EnumType.STRING)
3640
private AccountState state;
@@ -72,6 +76,14 @@ public void setType(AccountType type) {
7276
this.type = type;
7377
}
7478

79+
public AccountSource getSource() {
80+
return source;
81+
}
82+
83+
public void setSource(AccountSource source) {
84+
this.source = source;
85+
}
86+
7587
public AccountState getState() {
7688
return state;
7789
}

header/src/main/java/org/zstack/header/identity/AccountVO_.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class AccountVO_ extends ResourceVO_ {
1212
public static volatile SingularAttribute<AccountVO, String> description;
1313
public static volatile SingularAttribute<AccountVO, String> password;
1414
public static volatile SingularAttribute<AccountVO, AccountType> type;
15+
public static volatile SingularAttribute<AccountVO, AccountSource> source;
1516
public static volatile SingularAttribute<AccountVO, AccountState> state;
1617
public static volatile SingularAttribute<AccountVO, Timestamp> createDate;
1718
public static volatile SingularAttribute<AccountVO, Timestamp> lastOpDate;

header/src/main/java/org/zstack/header/identity/CreateAccountMsg.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class CreateAccountMsg extends NeedReplyMessage {
1313
@NoLogging
1414
private String password;
1515
private String type;
16+
private String source = AccountSource.Local.toString();
1617
private String description;
1718
private AccountState state;
1819

@@ -48,6 +49,14 @@ public void setType(String type) {
4849
this.type = type;
4950
}
5051

52+
public String getSource() {
53+
return source;
54+
}
55+
56+
public void setSource(String source) {
57+
this.source = source;
58+
}
59+
5160
public String getDescription() {
5261
return description;
5362
}

identity/src/main/java/org/zstack/identity/AccountManagerImpl.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ protected void scripts() {
110110
vo.setName(AccountConstant.INITIAL_SYSTEM_ADMIN_NAME);
111111
vo.setPassword(AccountConstant.INITIAL_SYSTEM_ADMIN_PASSWORD);
112112
vo.setType(AccountType.SystemAdmin);
113+
vo.setSource(AccountSource.Local);
113114
vo.setState(AccountState.Enabled);
114115
persist(vo);
115116
flush();
@@ -533,7 +534,21 @@ protected AccountInventory scripts() {
533534
vo.setName(msg.getName());
534535
vo.setDescription(msg.getDescription());
535536
vo.setPassword(msg.getPassword());
536-
vo.setType(msg.getType() != null ? AccountType.valueOf(msg.getType()) : AccountType.Normal);
537+
AccountType accountType = msg.getType() != null ? AccountType.valueOf(msg.getType()) : AccountType.Normal;
538+
if (accountType == AccountType.ThirdParty) {
539+
throw operr("account type[ThirdParty] is deprecated; use Normal with account source instead")
540+
.toException();
541+
}
542+
vo.setType(accountType);
543+
544+
try {
545+
vo.setSource(AccountSource.valueOf(msg.getSource()));
546+
} catch (IllegalArgumentException e) {
547+
throw operr("invalid account source[%s]", msg.getSource())
548+
.withOpaque("allowed.values", list(AccountSource.values()))
549+
.toException();
550+
}
551+
537552
vo.setState(msg.getState() == null ? AccountState.Enabled : msg.getState());
538553
persist(vo);
539554
reload(vo);
@@ -1005,6 +1020,11 @@ private void validate(APIChangeAccountTypeMsg msg) {
10051020
));
10061021
}
10071022

1023+
if (AccountType.ThirdParty.toString().equals(msg.getType())) {
1024+
throw new ApiMessageInterceptionException(argerr(
1025+
"account type[ThirdParty] is deprecated; use Normal with account source instead"));
1026+
}
1027+
10081028
if (!AccountType.SystemAdmin.toString().equals(msg.getType())) {
10091029
throw new ApiMessageInterceptionException(argerr(
10101030
"Only promoting to SystemAdmin is currently supported, got type[%s].", msg.getType()

identity/src/main/java/org/zstack/identity/rbac/RBACManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public void prepareDbInitialValue() {
177177
if (AccountConstant.OTHER_ROLE_UUID.equals(role.getUuid())
178178
|| AccountConstant.LEGACY_ROLE_UUID.equals(role.getUuid())) {
179179
List<String> accountUuidList = Q.New(AccountVO.class)
180-
.in(AccountVO_.type, list(AccountType.Normal, AccountType.ThirdParty))
180+
.eq(AccountVO_.type, AccountType.Normal)
181181
.select(AccountVO_.uuid)
182182
.listValues();
183183
for (String accountUuid : accountUuidList) {

0 commit comments

Comments
 (0)