Browse code
directory and user new pattern
Greg Wiley authored on 21/04/2017 00:06:45
Showing 5 changed files
Showing 5 changed files
... | ... |
@@ -5,21 +5,47 @@ |
5 | 5 |
#include <memory> |
6 | 6 |
#include <pwd.h> |
7 | 7 |
|
8 |
-class user |
|
8 |
+class user_ifc |
|
9 | 9 |
{ |
10 | 10 |
public: |
11 |
- virtual ~user() {} |
|
12 |
- // virtual std::string home_directory() = 0; |
|
11 |
+ virtual ~user_ifc() {} |
|
13 | 12 |
}; |
14 | 13 |
|
15 |
-typedef std::shared_ptr<user> user_p; |
|
16 |
-class directory |
|
14 |
+class user : public user_ifc |
|
17 | 15 |
{ |
16 |
+private: |
|
17 |
+ std::shared_ptr<user_ifc> delegate_; |
|
18 | 18 |
public: |
19 |
- virtual ~directory() {} |
|
20 |
- virtual const user_p find_user (const std::string &user_name) = 0; |
|
19 |
+ user (std::shared_ptr<user_ifc> delegate) : delegate_ (delegate) {} |
|
20 |
+ user() : user (std::shared_ptr<user_ifc> (new user_ifc)) {} |
|
21 |
+}; |
|
22 |
+ |
|
23 |
+class directory_ifc |
|
24 |
+{ |
|
25 |
+public: |
|
26 |
+ virtual ~directory_ifc() {} |
|
27 |
+ virtual std::vector<user> find_user (const std::string &user_name) |
|
28 |
+ { |
|
29 |
+ return std::vector<user>(); |
|
30 |
+ } |
|
31 |
+}; |
|
32 |
+ |
|
33 |
+class directory : public directory_ifc |
|
34 |
+{ |
|
35 |
+private: |
|
36 |
+ std::shared_ptr<directory_ifc> delegate_; |
|
37 |
+public: |
|
38 |
+ directory (std::shared_ptr<directory_ifc> delegate) : delegate_ |
|
39 |
+ (delegate) {} |
|
40 |
+ directory() : directory (std::shared_ptr<directory_ifc> |
|
41 |
+ (new directory_ifc)) {} |
|
42 |
+ std::vector<user> find_user (const std::string &user_name) |
|
43 |
+ { |
|
44 |
+ return delegate_->find_user (user_name); |
|
45 |
+ } |
|
46 |
+ |
|
47 |
+ static directory create(); |
|
21 | 48 |
}; |
22 | 49 |
|
23 |
-typedef std::shared_ptr<directory> directory_p; |
|
24 | 50 |
#endif |
25 | 51 |
|
... | ... |
@@ -1,50 +1,40 @@ |
1 | 1 |
#include <string> |
2 |
-#include <iostream> |
|
2 |
+#include <vector> |
|
3 | 3 |
|
4 | 4 |
#include "validator.h" |
5 | 5 |
|
6 | 6 |
namespace |
7 | 7 |
{ |
8 |
- class impl : public validator_ifc { |
|
9 |
- private: |
|
10 |
- directory_p directory_; |
|
11 |
- user_token_supplier_p user_token_supplier_; |
|
12 |
- public: |
|
13 |
- impl(const directory_p &directory, const user_token_supplier_p user_token_supplier) : |
|
14 |
- directory_(directory), |
|
15 |
- user_token_supplier_(user_token_supplier) {} |
|
16 |
- bool validate (const std::string &user_name, |
|
17 |
- const std::string &token) |
|
18 |
- { |
|
19 |
- user_p found_user = directory_->find_user (user_name); |
|
20 |
- |
|
21 |
- if (!found_user) { |
|
22 |
- return false; |
|
23 |
- } |
|
24 |
- |
|
25 |
- std::string user_token = user_token_supplier_->token (found_user); |
|
26 |
- return user_token == token; |
|
8 |
+class impl : public validator_ifc |
|
9 |
+{ |
|
10 |
+private: |
|
11 |
+ directory directory_; |
|
12 |
+ user_token_supplier_p user_token_supplier_; |
|
13 |
+public: |
|
14 |
+ impl (const directory &directory, |
|
15 |
+ const user_token_supplier_p user_token_supplier) : |
|
16 |
+ directory_ (directory), |
|
17 |
+ user_token_supplier_ (user_token_supplier) {} |
|
18 |
+ bool validate (const std::string &user_name, |
|
19 |
+ const std::string &token) |
|
20 |
+ { |
|
21 |
+ std::vector<user> found_user = directory_.find_user (user_name); |
|
22 |
+ |
|
23 |
+ if (found_user.empty()) { |
|
24 |
+ return false; |
|
27 | 25 |
} |
28 |
- }; |
|
29 |
-} |
|
30 | 26 |
|
31 |
-validator create_validator(const directory_p &directory, const user_token_supplier_p &user_token_supplier) { |
|
32 |
- std::shared_ptr<validator_ifc> delegate(new impl(directory, user_token_supplier)); |
|
33 |
- return validator(delegate); |
|
27 |
+ std::string user_token = user_token_supplier_->token (found_user[0]); |
|
28 |
+ return user_token == token; |
|
29 |
+ } |
|
30 |
+}; |
|
34 | 31 |
} |
35 | 32 |
|
36 |
-/* |
|
37 |
-bool old_validator::validate (const std::string &user_name, |
|
38 |
- const std::string &token) |
|
33 |
+validator create_validator (const directory &directory, |
|
34 |
+ const user_token_supplier_p &user_token_supplier) |
|
39 | 35 |
{ |
40 |
- user_p found_user = directory_->find_user (user_name); |
|
41 |
- |
|
42 |
- if (!found_user) { |
|
43 |
- return false; |
|
44 |
- } |
|
45 |
- |
|
46 |
- std::string user_token = user_token_supplier_->token (found_user); |
|
47 |
- return user_token == token; |
|
36 |
+ std::shared_ptr<validator_ifc> delegate (new impl (directory, |
|
37 |
+ user_token_supplier)); |
|
38 |
+ return validator (delegate); |
|
48 | 39 |
} |
49 |
-*/ |
|
50 | 40 |
|
... | ... |
@@ -1,22 +1,27 @@ |
1 |
+#include <string> |
|
2 |
+#include <vector> |
|
3 |
+ |
|
1 | 4 |
#include "validator.h" |
2 | 5 |
#include "user.h" |
3 | 6 |
#include "test_util.h" |
4 | 7 |
#include "token.h" |
5 | 8 |
|
6 |
-class fake_directory : public directory |
|
9 |
+class fake_directory : public directory_ifc |
|
7 | 10 |
{ |
8 | 11 |
private: |
9 | 12 |
std::string user_name_; |
10 | 13 |
public: |
11 |
- fake_directory (const std::string &user_name) : user_name_ (user_name) {} |
|
14 |
+ fake_directory (const std::string &user_name) : user_name_ (user_name) |
|
15 |
+ { |
|
16 |
+ } |
|
12 | 17 |
fake_directory() : user_name_ ("_NOT_A_USER") {} |
13 | 18 |
|
14 |
- virtual const user_p find_user (const std::string &user_name) |
|
19 |
+ std::vector<user> find_user (const std::string &user_name) |
|
15 | 20 |
{ |
16 |
- user_p result; |
|
21 |
+ std::vector<user> result; |
|
17 | 22 |
|
18 | 23 |
if (user_name == user_name_) { |
19 |
- result.reset (new user); |
|
24 |
+ result.push_back (user()); |
|
20 | 25 |
} |
21 | 26 |
|
22 | 27 |
return result; |
... | ... |
@@ -30,12 +35,18 @@ private: |
30 | 35 |
public: |
31 | 36 |
fake_user_token_supplier (const std::string &token) : token_ (token) {} |
32 | 37 |
fake_user_token_supplier() : token_ ("_NOT_A_TOKEN") {} |
33 |
- virtual std::string token (const user_p user) |
|
38 |
+ virtual std::string token (user user) |
|
34 | 39 |
{ |
35 | 40 |
return token_; |
36 | 41 |
} |
37 | 42 |
}; |
38 | 43 |
|
44 |
+template<class T> |
|
45 |
+std::shared_ptr<T> share (T *t) |
|
46 |
+{ |
|
47 |
+ return std::shared_ptr<T> (t); |
|
48 |
+} |
|
49 |
+ |
|
39 | 50 |
bool validator_validates() |
40 | 51 |
{ |
41 | 52 |
|
... | ... |
@@ -44,7 +55,7 @@ bool validator_validates() |
44 | 55 |
user_token_supplier_p user_token_supplier (new fake_user_token_supplier ( |
45 | 56 |
token)); |
46 | 57 |
std::string user_name = "msmith"; |
47 |
- directory_p directory (new fake_directory (user_name)); |
|
58 |
+ directory directory (share (new fake_directory (user_name))); |
|
48 | 59 |
validator validator = create_validator (directory, user_token_supplier); |
49 | 60 |
|
50 | 61 |
// when |
... | ... |
@@ -62,7 +73,7 @@ bool validator_fails_unknown_user() |
62 | 73 |
std::string token = "token"; |
63 | 74 |
user_token_supplier_p user_token_supplier (new fake_user_token_supplier ( |
64 | 75 |
token)); |
65 |
- directory_p directory (new fake_directory); |
|
76 |
+ directory directory (share (new fake_directory)); |
|
66 | 77 |
validator validator = create_validator (directory, user_token_supplier); |
67 | 78 |
|
68 | 79 |
// when |
... | ... |
@@ -79,7 +90,7 @@ bool validator_fails_incorrect_token() |
79 | 90 |
// given |
80 | 91 |
user_token_supplier_p user_token_supplier (new fake_user_token_supplier); |
81 | 92 |
std::string user_name = "msmith"; |
82 |
- directory_p directory (new fake_directory (user_name)); |
|
93 |
+ directory directory (share (new fake_directory (user_name))); |
|
83 | 94 |
validator validator = create_validator (directory, user_token_supplier); |
84 | 95 |
|
85 | 96 |
// when |