@@ -6,6 +6,8 @@ pub struct User {
6
6
pub name : String ,
7
7
// password is optional
8
8
pub password : Option < String > ,
9
+ // Need to update password at anytime? by default is false
10
+ pub update_password : Option < bool > ,
9
11
pub roles : Vec < String > ,
10
12
}
11
13
@@ -19,9 +21,17 @@ impl User {
19
21
format ! ( "CREATE USER {}{};" , self . name, password)
20
22
}
21
23
24
+ pub fn to_sql_update ( & self ) -> String {
25
+ let password = match & self . password {
26
+ Some ( p) => format ! ( " WITH PASSWORD '{}'" , p) ,
27
+ None => "" . to_string ( ) ,
28
+ } ;
29
+
30
+ format ! ( "ALTER USER {}{};" , self . name, password)
31
+ }
32
+
22
33
pub fn to_sql_drop ( & self ) -> String {
23
- let sql = format ! ( "DROP USER IF EXISTS {};" , self . name) ;
24
- sql
34
+ format ! ( "DROP USER IF EXISTS {};" , self . name)
25
35
}
26
36
27
37
pub fn validate ( & self ) -> Result < ( ) > {
@@ -58,18 +68,33 @@ mod tests {
58
68
let user = User {
59
69
name : "test" . to_string ( ) ,
60
70
password : Some ( "test" . to_string ( ) ) ,
71
+ update_password : Some ( true ) ,
61
72
roles : vec ! [ "test" . to_string( ) ] ,
62
73
} ;
63
74
64
75
let sql = user. to_sql_create ( ) ;
65
76
assert_eq ! ( sql, "CREATE USER test WITH PASSWORD 'test';" ) ;
66
77
}
67
78
79
+ #[ test]
80
+ fn test_user_to_sql_update ( ) {
81
+ let user = User {
82
+ name : "test" . to_string ( ) ,
83
+ password : Some ( "test" . to_string ( ) ) ,
84
+ update_password : Some ( true ) ,
85
+ roles : vec ! [ "test" . to_string( ) ] ,
86
+ } ;
87
+
88
+ let sql = user. to_sql_update ( ) ;
89
+ assert_eq ! ( sql, "ALTER USER test WITH PASSWORD 'test';" ) ;
90
+ }
91
+
68
92
#[ test]
69
93
fn test_user_to_sql_drop ( ) {
70
94
let user = User {
71
95
name : "test" . to_string ( ) ,
72
96
password : Some ( "test" . to_string ( ) ) ,
97
+ update_password : Some ( true ) ,
73
98
roles : vec ! [ "test" . to_string( ) ] ,
74
99
} ;
75
100
@@ -82,6 +107,7 @@ mod tests {
82
107
let user = User {
83
108
name : "test" . to_string ( ) ,
84
109
password : Some ( "test" . to_string ( ) ) ,
110
+ update_password : Some ( true ) ,
85
111
roles : vec ! [ "test" . to_string( ) ] ,
86
112
} ;
87
113
@@ -93,6 +119,7 @@ mod tests {
93
119
let user = User {
94
120
name : "" . to_string ( ) ,
95
121
password : Some ( "test" . to_string ( ) ) ,
122
+ update_password : Some ( true ) ,
96
123
roles : vec ! [ "test" . to_string( ) ] ,
97
124
} ;
98
125
@@ -104,6 +131,7 @@ mod tests {
104
131
let user = User {
105
132
name : "test" . to_string ( ) ,
106
133
password : None ,
134
+ update_password : Some ( true ) ,
107
135
roles : vec ! [ "test" . to_string( ) ] ,
108
136
} ;
109
137
@@ -115,6 +143,7 @@ mod tests {
115
143
let user = User {
116
144
name : "test" . to_string ( ) ,
117
145
password : Some ( "test" . to_string ( ) ) ,
146
+ update_password : Some ( true ) ,
118
147
roles : vec ! [ ] ,
119
148
} ;
120
149
@@ -126,6 +155,7 @@ mod tests {
126
155
let user = User {
127
156
name : "test" . to_string ( ) ,
128
157
password : Some ( "test" . to_string ( ) ) ,
158
+ update_password : Some ( true ) ,
129
159
roles : vec ! [ "test" . to_string( ) ] ,
130
160
} ;
131
161
@@ -137,6 +167,7 @@ mod tests {
137
167
let user = User {
138
168
name : "test" . to_string ( ) ,
139
169
password : Some ( "test" . to_string ( ) ) ,
170
+ update_password : Some ( true ) ,
140
171
roles : vec ! [ "test" . to_string( ) ] ,
141
172
} ;
142
173
@@ -148,6 +179,7 @@ mod tests {
148
179
let user = User {
149
180
name : "test" . to_string ( ) ,
150
181
password : Some ( "test" . to_string ( ) ) ,
182
+ update_password : Some ( true ) ,
151
183
roles : vec ! [ "test" . to_string( ) ] ,
152
184
} ;
153
185
0 commit comments