فهرست منبع

User infos are passed to the view so it can be used to display customized infos + Fixing a bug where bim-ng would crash when a user with no group logged on

clement 1 سال پیش
والد
کامیت
4a469eaa0a
3فایلهای تغییر یافته به همراه34 افزوده شده و 11 حذف شده
  1. 22 5
      src/handler.rs
  2. 8 5
      src/ldap/lib.rs
  3. 4 1
      templates/home.html

+ 22 - 5
src/handler.rs

@@ -17,10 +17,25 @@ pub struct FormChangePasswd {
     new_password_conf: String,
 }
 
-async fn get_template(template_name: String) -> String {
+async fn get_template(template_name: String, session: Session) -> String {
+    let username: Option<String> = session.get("user_id")
+    .unwrap_or(None);
+    let user_is_ssh: Option<bool> = session.get("user_is_ssh")
+    .unwrap_or(None);
+
     let tera = Tera::new("templates/*.html")
     .expect("Failed to parse template files");
-    let ctx = tera::Context::new();
+
+    let mut ctx = tera::Context::new();
+    match username {
+        Some(username) => ctx.insert("username", &username),
+        None => (),
+    }
+    match user_is_ssh {
+        Some(user_is_ssh) => ctx.insert("user_is_ssh", &user_is_ssh),
+        None => (),
+    }
+
     tera.render(&template_name, &ctx)
     .expect(format!("Faile to render template {}", template_name).as_str())
 }
@@ -47,7 +62,7 @@ pub async fn index(session: Session) -> impl Responder {
         .finish();
     }
 
-    let body = get_template("signin.html".to_string()).await;
+    let body = get_template("signin.html".to_string(), session).await;
     HttpResponse::Ok().content_type("text/html")
     .body(body)
 }
@@ -58,6 +73,8 @@ pub async fn auth(ldap_wrapper: web::Data<LdapWrapper>, form: web::Form<FormLogi
     .await {
         Ok(user) => {
             session.insert("user_id", user.uid).unwrap();
+            session.insert("user_is_ssh", user.is_ssh).unwrap();
+            session.insert("user_groups", user.groups).unwrap();
             return HttpResponse::Ok()
             .status(StatusCode::FOUND)
             .append_header((header::LOCATION, "/home"))
@@ -74,7 +91,7 @@ pub async fn home(session: Session) -> impl Responder {
         .append_header((header::LOCATION, "/"))
         .finish();
     }
-    let body = get_template("home.html".to_string()).await;
+    let body = get_template("home.html".to_string(), session).await;
     HttpResponse::Ok().content_type("text/html")
     .body(body)
 }
@@ -86,7 +103,7 @@ pub async fn form_password(session: Session) -> impl Responder {
         .append_header((header::LOCATION, "/"))
         .finish();
     }
-    let body = get_template("formpassword.html".to_string()).await;
+    let body = get_template("formpassword.html".to_string(), session).await;
     HttpResponse::Ok().content_type("text/html")
     .body(body)
 }

+ 8 - 5
src/ldap/lib.rs

@@ -46,11 +46,14 @@ impl LdapWrapper {
         let records = SearchEntry::construct(result_entry.first().unwrap().to_owned());
 
         let is_ssh = records.attrs.get("objectClass").unwrap().contains(&"ldapPublicKey".to_string());
-        let groups: Vec<String> = records.attrs.get("memberOf").unwrap().iter()
-        .map(|elem| {
-            elem.replace(&format!(",{}", self.config.groupsdn).to_string(), "")
-            .replace("cn=", "")
-        }).collect();
+        let groups: Vec<String> = match records.attrs.get("memberOf") {
+            Some(memberof_records) => memberof_records.iter()
+            .map(|elem| {
+                elem.replace(&format!(",{}", self.config.groupsdn).to_string(), "")
+                .replace("cn=", "")
+            }).collect(),
+            None => Vec::new(),
+        };
 
         Ok((is_ssh, groups))
     }

+ 4 - 1
templates/home.html

@@ -4,10 +4,13 @@
         <title>BIM</title>
     </head>
     <body>
-        <h1>Hello</h1>
+        <h1>Hello {{ username }}</h1>
         <ul>
             <li><a href="changepassword">Change password</a></li>
             <li><a href="signout">Sign out</a></li>
+            {% if user_is_ssh %}
+            <li><a href="ssh">Manage SSH Keys</a></li>
+            {% endif %}
         </ul>
     </body>
 </html>