|
1 |
| -// Copyright 2012,2013 Vaughn Vernon |
2 |
| -// |
3 |
| -// Licensed under the Apache License, Version 2.0 (the "License"); |
4 |
| -// you may not use this file except in compliance with the License. |
5 |
| -// You may obtain a copy of the License at |
6 |
| -// |
7 |
| -// http://www.apache.org/licenses/LICENSE-2.0 |
8 |
| -// |
9 |
| -// Unless required by applicable law or agreed to in writing, software |
10 |
| -// distributed under the License is distributed on an "AS IS" BASIS, |
11 |
| -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 |
| -// See the License for the specific language governing permissions and |
13 |
| -// limitations under the License. |
14 |
| - |
15 |
| -namespace SaaSOvation.IdentityAccess.Domain.Model.Identity |
16 |
| -{ |
17 |
| - using System; |
18 |
| - using System.Collections.Generic; |
19 |
| - using System.Linq; |
20 |
| - using SaaSOvation.Common.Domain.Model; |
21 |
| - using SaaSOvation.IdentityAccess.Domain.Model.Access; |
22 |
| - |
23 |
| - public class Tenant |
24 |
| - { |
25 |
| - public Tenant(string name, string description, bool active) |
26 |
| - { |
27 |
| - this.Active = active; |
28 |
| - this.Description = description; |
29 |
| - this.Name = name; |
30 |
| - this.registrationInvitations = new HashSet<RegistrationInvitation>(); |
31 |
| - this.TenantId = new TenantId(); |
32 |
| - } |
33 |
| - |
34 |
| - public bool Active { get; private set; } |
35 |
| - |
36 |
| - public string Description { get; private set; } |
37 |
| - |
38 |
| - public string Name { get; private set; } |
39 |
| - |
40 |
| - public TenantId TenantId { get; private set; } |
41 |
| - |
42 |
| - ISet<RegistrationInvitation> registrationInvitations; |
43 |
| - |
44 |
| - public void Activate() |
45 |
| - { |
46 |
| - if (!this.Active) |
47 |
| - { |
48 |
| - this.Active = true; |
49 |
| - DomainEventPublisher.Instance.Publish(new TenantActivated(this.TenantId)); |
50 |
| - } |
51 |
| - } |
52 |
| - |
53 |
| - public ICollection<InvitationDescriptor> AllAvailableRegistrationInvitations() |
54 |
| - { |
55 |
| - AssertionConcern.AssertStateTrue(this.Active, "Tenant is not active."); |
56 |
| - return AllRegistrationInvitationsFor(true); |
57 |
| - } |
58 |
| - |
59 |
| - public ICollection<InvitationDescriptor> AllUnavailableRegistrationInvitations() |
60 |
| - { |
61 |
| - AssertionConcern.AssertStateTrue(this.Active, "Tenant is not active."); |
62 |
| - return AllRegistrationInvitationsFor(false); |
63 |
| - } |
64 |
| - |
65 |
| - public void Deactivate() |
66 |
| - { |
67 |
| - if (this.Active) |
68 |
| - { |
69 |
| - this.Active = false; |
70 |
| - DomainEventPublisher.Instance.Publish(new TenantDeactivated(this.TenantId)); |
71 |
| - } |
72 |
| - } |
73 |
| - |
74 |
| - public bool IsRegistrationAvailableThrough(string invitationIdentifier) |
75 |
| - { |
76 |
| - AssertionConcern.AssertStateTrue(this.Active, "Tenant is not active."); |
77 |
| - var invitation = InvitationOf(invitationIdentifier); |
78 |
| - return invitation == null ? false : invitation.IsAvailable(); |
79 |
| - } |
80 |
| - |
81 |
| - public RegistrationInvitation OfferRegistrationInvitation(string description) |
82 |
| - { |
83 |
| - AssertionConcern.AssertStateTrue(this.Active, "Tenant is not active."); |
84 |
| - AssertionConcern.AssertArgumentTrue(this.IsRegistrationAvailableThrough(description), "Invitation already exists."); |
85 |
| - |
86 |
| - var invitation = new RegistrationInvitation(this.TenantId, new Guid().ToString(), description); |
87 |
| - |
88 |
| - AssertionConcern.AssertStateTrue(this.registrationInvitations.Add(invitation), "The invitation should have been added."); |
89 |
| - |
90 |
| - return invitation; |
91 |
| - } |
92 |
| - |
93 |
| - public Group ProvisionGroup(string name, string description) |
94 |
| - { |
95 |
| - AssertionConcern.AssertStateTrue(this.Active, "Tenant is not active."); |
96 |
| - |
97 |
| - var group = new Group(this.TenantId, name, description); |
98 |
| - |
99 |
| - DomainEventPublisher.Instance.Publish(new GroupProvisioned(this.TenantId, name)); |
100 |
| - |
101 |
| - return group; |
102 |
| - } |
103 |
| - |
104 |
| - public Role ProvisionRole(string name, string description) |
105 |
| - { |
106 |
| - return ProvisionRole(name, description, false); |
107 |
| - } |
108 |
| - |
109 |
| - Role ProvisionRole(string name, string description, bool supportsNesting) |
110 |
| - { |
111 |
| - AssertionConcern.AssertStateTrue(this.Active, "Tenant is not active."); |
112 |
| - |
113 |
| - var role = new Role(this.TenantId, name, description, supportsNesting); |
114 |
| - |
115 |
| - DomainEventPublisher.Instance.Publish(new RoleProvisioned(this.TenantId, name)); |
116 |
| - |
117 |
| - return role; |
118 |
| - } |
119 |
| - |
120 |
| - public RegistrationInvitation RedefineRegistrationInvitationAs(string invitationIdentifier) |
121 |
| - { |
122 |
| - AssertionConcern.AssertStateTrue(this.Active, "Tenant is not active."); |
123 |
| - var invitation = InvitationOf(invitationIdentifier); |
124 |
| - if (invitation != null) |
125 |
| - { |
126 |
| - invitation.RedefineAs().OpenEnded(); |
127 |
| - } |
128 |
| - return invitation; |
129 |
| - } |
130 |
| - |
131 |
| - public User RegisterUser(string invitationIdentifier, string username, string password, Enablement enablement, Person person) |
132 |
| - { |
133 |
| - AssertionConcern.AssertStateTrue(this.Active, "Tenant is not active."); |
134 |
| - User user = null; |
135 |
| - if (IsRegistrationAvailableThrough(invitationIdentifier)) |
136 |
| - { |
137 |
| - // ensure same tenant |
138 |
| - person.TenantId = this.TenantId; |
139 |
| - user = new User(this.TenantId, username, password, enablement, person); |
140 |
| - } |
141 |
| - return user; |
142 |
| - } |
143 |
| - |
144 |
| - public void WithdrawInvitation(string invitationIdentifier) |
145 |
| - { |
146 |
| - var invitation = InvitationOf(invitationIdentifier); |
147 |
| - if (invitation != null) |
148 |
| - { |
149 |
| - this.registrationInvitations.Remove(invitation); |
150 |
| - } |
151 |
| - } |
152 |
| - |
153 |
| - List<InvitationDescriptor> AllRegistrationInvitationsFor(bool isAvailable) |
154 |
| - { |
155 |
| - return this.registrationInvitations |
156 |
| - .Where(x => x.IsAvailable() == isAvailable) |
157 |
| - .Select(x => x.ToDescriptor()) |
158 |
| - .ToList(); |
159 |
| - } |
160 |
| - |
161 |
| - RegistrationInvitation InvitationOf(string invitationIdentifier) |
162 |
| - { |
163 |
| - return this.registrationInvitations.FirstOrDefault(x => x.IsIdentifiedBy(invitationIdentifier)); |
164 |
| - } |
165 |
| - |
166 |
| - public override bool Equals(object anotherObject) |
167 |
| - { |
168 |
| - var equalObjects = false; |
169 |
| - if (anotherObject != null && this.GetType() == anotherObject.GetType()) |
170 |
| - { |
171 |
| - var typedObject = (Tenant)anotherObject; |
172 |
| - equalObjects = |
173 |
| - this.TenantId.Equals(typedObject.TenantId) && |
174 |
| - this.Name.Equals(typedObject.Name); |
175 |
| - } |
176 |
| - return equalObjects; |
177 |
| - } |
178 |
| - |
179 |
| - public override int GetHashCode() |
180 |
| - { |
181 |
| - return |
182 |
| - +(48123 * 257) |
183 |
| - + this.TenantId.GetHashCode() |
184 |
| - + this.Name.GetHashCode(); |
185 |
| - } |
186 |
| - |
187 |
| - public override string ToString() |
188 |
| - { |
189 |
| - return "Tenant [" |
190 |
| - + ", tenantId=" + TenantId |
191 |
| - + ", name=" + Name |
192 |
| - + ", description=" + Description |
193 |
| - + ", active=" + Active |
194 |
| - + "]"; |
195 |
| - } |
196 |
| - } |
197 |
| -} |
| 1 | +// Copyright 2012,2013 Vaughn Vernon |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +namespace SaaSOvation.IdentityAccess.Domain.Model.Identity |
| 16 | +{ |
| 17 | + using System; |
| 18 | + using System.Collections.Generic; |
| 19 | + using System.Linq; |
| 20 | + using SaaSOvation.Common.Domain.Model; |
| 21 | + using SaaSOvation.IdentityAccess.Domain.Model.Access; |
| 22 | + |
| 23 | + public class Tenant |
| 24 | + { |
| 25 | + public Tenant(string name, string description, bool active) |
| 26 | + { |
| 27 | + this.Active = active; |
| 28 | + this.Description = description; |
| 29 | + this.Name = name; |
| 30 | + this.registrationInvitations = new HashSet<RegistrationInvitation>(); |
| 31 | + this.TenantId = new TenantId(); |
| 32 | + } |
| 33 | + |
| 34 | + public bool Active { get; private set; } |
| 35 | + |
| 36 | + public string Description { get; private set; } |
| 37 | + |
| 38 | + public string Name { get; private set; } |
| 39 | + |
| 40 | + public TenantId TenantId { get; private set; } |
| 41 | + |
| 42 | + ISet<RegistrationInvitation> registrationInvitations; |
| 43 | + |
| 44 | + public void Activate() |
| 45 | + { |
| 46 | + if (!this.Active) |
| 47 | + { |
| 48 | + this.Active = true; |
| 49 | + DomainEventPublisher.Instance.Publish(new TenantActivated(this.TenantId)); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public ICollection<InvitationDescriptor> AllAvailableRegistrationInvitations() |
| 54 | + { |
| 55 | + AssertionConcern.AssertStateTrue(this.Active, "Tenant is not active."); |
| 56 | + return AllRegistrationInvitationsFor(true); |
| 57 | + } |
| 58 | + |
| 59 | + public ICollection<InvitationDescriptor> AllUnavailableRegistrationInvitations() |
| 60 | + { |
| 61 | + AssertionConcern.AssertStateTrue(this.Active, "Tenant is not active."); |
| 62 | + return AllRegistrationInvitationsFor(false); |
| 63 | + } |
| 64 | + |
| 65 | + public void Deactivate() |
| 66 | + { |
| 67 | + if (this.Active) |
| 68 | + { |
| 69 | + this.Active = false; |
| 70 | + DomainEventPublisher.Instance.Publish(new TenantDeactivated(this.TenantId)); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public bool IsRegistrationAvailableThrough(string invitationIdentifier) |
| 75 | + { |
| 76 | + AssertionConcern.AssertStateTrue(this.Active, "Tenant is not active."); |
| 77 | + var invitation = InvitationOf(invitationIdentifier); |
| 78 | + return invitation == null ? false : invitation.IsAvailable(); |
| 79 | + } |
| 80 | + |
| 81 | + public RegistrationInvitation OfferRegistrationInvitation(string description) |
| 82 | + { |
| 83 | + AssertionConcern.AssertStateTrue(this.Active, "Tenant is not active."); |
| 84 | + AssertionConcern.AssertArgumentTrue(IsRegistrationAvailableThrough(description), "Invitation already exists."); |
| 85 | + |
| 86 | + var invitation = new RegistrationInvitation(this.TenantId, new Guid().ToString(), description); |
| 87 | + |
| 88 | + AssertionConcern.AssertStateTrue(this.registrationInvitations.Add(invitation), "The invitation should have been added."); |
| 89 | + |
| 90 | + return invitation; |
| 91 | + } |
| 92 | + |
| 93 | + public Group ProvisionGroup(string name, string description) |
| 94 | + { |
| 95 | + AssertionConcern.AssertStateTrue(this.Active, "Tenant is not active."); |
| 96 | + |
| 97 | + var group = new Group(this.TenantId, name, description); |
| 98 | + |
| 99 | + DomainEventPublisher.Instance.Publish(new GroupProvisioned(this.TenantId, name)); |
| 100 | + |
| 101 | + return group; |
| 102 | + } |
| 103 | + |
| 104 | + public Role ProvisionRole(string name, string description) |
| 105 | + { |
| 106 | + return ProvisionRole(name, description, false); |
| 107 | + } |
| 108 | + |
| 109 | + Role ProvisionRole(string name, string description, bool supportsNesting) |
| 110 | + { |
| 111 | + AssertionConcern.AssertStateTrue(this.Active, "Tenant is not active."); |
| 112 | + |
| 113 | + var role = new Role(this.TenantId, name, description, supportsNesting); |
| 114 | + |
| 115 | + DomainEventPublisher.Instance.Publish(new RoleProvisioned(this.TenantId, name)); |
| 116 | + |
| 117 | + return role; |
| 118 | + } |
| 119 | + |
| 120 | + public RegistrationInvitation RedefineRegistrationInvitationAs(string invitationIdentifier) |
| 121 | + { |
| 122 | + AssertionConcern.AssertStateTrue(this.Active, "Tenant is not active."); |
| 123 | + var invitation = InvitationOf(invitationIdentifier); |
| 124 | + if (invitation != null) |
| 125 | + { |
| 126 | + invitation.RedefineAs().OpenEnded(); |
| 127 | + } |
| 128 | + return invitation; |
| 129 | + } |
| 130 | + |
| 131 | + public User RegisterUser(string invitationIdentifier, string username, string password, Enablement enablement, Person person) |
| 132 | + { |
| 133 | + AssertionConcern.AssertStateTrue(this.Active, "Tenant is not active."); |
| 134 | + User user = null; |
| 135 | + if (IsRegistrationAvailableThrough(invitationIdentifier)) |
| 136 | + { |
| 137 | + // ensure same tenant |
| 138 | + person.TenantId = this.TenantId; |
| 139 | + user = new User(this.TenantId, username, password, enablement, person); |
| 140 | + } |
| 141 | + return user; |
| 142 | + } |
| 143 | + |
| 144 | + public void WithdrawInvitation(string invitationIdentifier) |
| 145 | + { |
| 146 | + var invitation = InvitationOf(invitationIdentifier); |
| 147 | + if (invitation != null) |
| 148 | + { |
| 149 | + this.registrationInvitations.Remove(invitation); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + List<InvitationDescriptor> AllRegistrationInvitationsFor(bool isAvailable) |
| 154 | + { |
| 155 | + return this.registrationInvitations |
| 156 | + .Where(x => x.IsAvailable() == isAvailable) |
| 157 | + .Select(x => x.ToDescriptor()) |
| 158 | + .ToList(); |
| 159 | + } |
| 160 | + |
| 161 | + RegistrationInvitation InvitationOf(string invitationIdentifier) |
| 162 | + { |
| 163 | + return this.registrationInvitations.FirstOrDefault(x => x.IsIdentifiedBy(invitationIdentifier)); |
| 164 | + } |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | + public override bool Equals(object anotherObject) |
| 169 | + { |
| 170 | + var equalObjects = false; |
| 171 | + if (anotherObject != null && this.GetType() == anotherObject.GetType()) |
| 172 | + { |
| 173 | + var typedObject = (Tenant)anotherObject; |
| 174 | + equalObjects = |
| 175 | + this.TenantId.Equals(typedObject.TenantId) && |
| 176 | + this.Name.Equals(typedObject.Name); |
| 177 | + } |
| 178 | + return equalObjects; |
| 179 | + } |
| 180 | + |
| 181 | + public override int GetHashCode() |
| 182 | + { |
| 183 | + return |
| 184 | + +(48123 * 257) |
| 185 | + + this.TenantId.GetHashCode() |
| 186 | + + this.Name.GetHashCode(); |
| 187 | + } |
| 188 | + |
| 189 | + public override string ToString() |
| 190 | + { |
| 191 | + return "Tenant [" |
| 192 | + + ", tenantId=" + TenantId |
| 193 | + + ", name=" + Name |
| 194 | + + ", description=" + Description |
| 195 | + + ", active=" + Active |
| 196 | + + "]"; |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments