2017-02-13 20:20:35 +01:00
|
|
|
/**
|
|
|
|
* Poodinis Dependency Injection Framework
|
2024-02-17 13:08:59 +01:00
|
|
|
* Copyright 2014-2024 Mike Bierlee
|
2017-02-13 20:20:35 +01:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* The full terms of the license can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import poodinis;
|
|
|
|
|
|
|
|
import std.random;
|
|
|
|
import std.digest.md;
|
|
|
|
import std.stdio;
|
|
|
|
import std.conv;
|
|
|
|
|
2023-03-06 23:24:18 +01:00
|
|
|
class SecurityAuditor {
|
2023-05-17 23:03:02 +02:00
|
|
|
void submitAudit() {
|
2017-02-13 20:20:35 +01:00
|
|
|
writeln("Hmmmyes I have received your audit. It is.... adequate.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 23:24:18 +01:00
|
|
|
class SuperSecurityDevice {
|
2017-02-13 20:20:35 +01:00
|
|
|
private int seed;
|
|
|
|
|
2023-05-17 23:03:02 +02:00
|
|
|
this() {
|
2017-02-13 20:20:35 +01:00
|
|
|
auto randomGenerator = Random(unpredictableSeed);
|
|
|
|
seed = uniform(0, 999, randomGenerator);
|
|
|
|
}
|
|
|
|
|
2023-05-17 23:03:02 +02:00
|
|
|
string getPassword() {
|
2017-02-13 20:20:35 +01:00
|
|
|
return to!string(seed) ~ "t1m3sp13!!:";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 23:24:18 +01:00
|
|
|
class SecurityManager {
|
2023-03-07 01:29:54 +01:00
|
|
|
@Inject private SuperSecurityDevice levelOneSecurity;
|
2017-02-13 20:20:35 +01:00
|
|
|
|
2023-03-07 01:29:54 +01:00
|
|
|
@Inject @AssignNewInstance private SuperSecurityDevice levelTwoSecurity;
|
2017-02-13 20:20:35 +01:00
|
|
|
|
2023-03-07 01:29:54 +01:00
|
|
|
@Inject @OptionalDependency private SecurityAuditor auditor;
|
2017-02-13 20:20:35 +01:00
|
|
|
|
2023-05-17 23:03:02 +02:00
|
|
|
void doAudit() {
|
2023-03-06 23:24:18 +01:00
|
|
|
if (auditor !is null) {
|
2017-02-13 20:20:35 +01:00
|
|
|
auditor.submitAudit();
|
2023-03-06 23:24:18 +01:00
|
|
|
} else {
|
2017-02-13 20:20:35 +01:00
|
|
|
writeln("I uh, will skip the audit for now...");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 23:24:18 +01:00
|
|
|
void main() {
|
2017-02-13 20:20:35 +01:00
|
|
|
auto dependencies = new shared DependencyContainer();
|
|
|
|
dependencies.register!SuperSecurityDevice; // Registered with the default "Single instance" scope
|
|
|
|
dependencies.register!SecurityManager;
|
|
|
|
|
|
|
|
auto manager = dependencies.resolve!SecurityManager;
|
|
|
|
|
|
|
|
writeln("Password for user one: " ~ manager.levelOneSecurity.getPassword());
|
|
|
|
writeln("Password for user two: " ~ manager.levelTwoSecurity.getPassword());
|
|
|
|
|
2023-03-06 23:24:18 +01:00
|
|
|
if (manager.levelOneSecurity is manager.levelTwoSecurity) {
|
2017-02-13 20:20:35 +01:00
|
|
|
writeln("SECURITY BREACH!!!!!"); // Should not be printed since levelTwoSecurity is a new instance.
|
2023-03-06 23:24:18 +01:00
|
|
|
} else {
|
2017-02-13 20:20:35 +01:00
|
|
|
writeln("Security okay!");
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.doAudit(); // Will not cause the SecurityAuditor to print, since we didn't register a SecurityAuditor.
|
|
|
|
}
|