Skip to content

Commit 5a96d8e

Browse files
author
Alena Prokharchyk
committed
CLOUDSTACK-7073: Added domainId field to the user table in order to restrict duplicated users creation on the db level
1 parent b87a5ce commit 5a96d8e

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

engine/schema/src/com/cloud/user/UserVO.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ public class UserVO implements User, Identity, InternalIdentity {
9797
@Column(name = "default")
9898
boolean isDefault;
9999

100+
@Column(name = "domain_id")
101+
private long domainId;
102+
100103
public UserVO() {
101104
this.uuid = UUID.randomUUID().toString();
102105
}
@@ -270,4 +273,7 @@ public boolean isDefault() {
270273
return isDefault;
271274
}
272275

276+
public void setDomainId(long domainId) {
277+
this.domainId = domainId;
278+
}
273279
}

engine/schema/src/com/cloud/user/dao/UserDaoImpl.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
import java.util.List;
2020

2121
import javax.ejb.Local;
22+
import javax.inject.Inject;
2223

2324
import org.springframework.stereotype.Component;
2425

26+
import com.cloud.user.Account;
2527
import com.cloud.user.UserVO;
2628
import com.cloud.utils.db.DB;
2729
import com.cloud.utils.db.GenericDaoBase;
@@ -40,6 +42,9 @@ public class UserDaoImpl extends GenericDaoBase<UserVO, Long> implements UserDao
4042
protected SearchBuilder<UserVO> SecretKeySearch;
4143
protected SearchBuilder<UserVO> RegistrationTokenSearch;
4244

45+
@Inject
46+
AccountDao _accountDao;
47+
4348
protected UserDaoImpl() {
4449
UsernameSearch = createSearchBuilder();
4550
UsernameSearch.and("username", UsernameSearch.entity().getUsername(), SearchCriteria.Op.EQ);
@@ -128,4 +133,12 @@ public List<UserVO> findUsersByName(String username) {
128133
return listBy(sc);
129134
}
130135

136+
@Override
137+
@DB
138+
public UserVO persist(UserVO user) {
139+
Account account = _accountDao.findById(user.getAccountId());
140+
user.setDomainId(account.getDomainId());
141+
return super.persist(user);
142+
}
143+
131144
}

setup/db/db/schema-440to450.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,9 @@ CREATE VIEW `cloud`.`volume_view` AS
234234

235235
/* As part of the separation of Xen and XenServer, update the column for the network labels */
236236
ALTER TABLE `cloud`.`physical_network_traffic_types` CHANGE `xen_network_label` `xenserver_network_label` varchar(255) COMMENT 'The network name label of the physical device dedicated to this traffic on a XenServer host';
237+
238+
/*Adding domainId field to the user table in order to restrict duplicated users creation on the db level*/
239+
ALTER TABLE `cloud`.`user` ADD COLUMN domain_id bigint(20) unsigned DEFAULT NULL;
240+
ALTER TABLE `cloud`.`user` ADD CONSTRAINT `fk_user__domain_id` FOREIGN KEY `fk_user__domain_id`(`domain_id`) REFERENCES `domain`(`id`) ON DELETE CASCADE;
241+
UPDATE `cloud`.`user` SET `cloud`.`user`.domain_id=(SELECT `cloud`.`account`.domain_id FROM `cloud`.`account` WHERE `cloud`.`account`.id=`cloud`.`user`.account_id) where id > 0;
242+
ALTER TABLE `cloud`.`user` ADD UNIQUE KEY `username_domain_id` (`username`,`domain_id`);

0 commit comments

Comments
 (0)