Dasflugen 5 سال پیش
والد
کامیت
8e26c0f0ca
2فایلهای تغییر یافته به همراه147 افزوده شده و 119 حذف شده
  1. 48 21
      api/authorization.go
  2. 99 98
      schedule/xlsx.go

+ 48 - 21
api/authorization.go

@@ -1,26 +1,53 @@
 package api
 
-// import(
-// 	"github.com/jtblin/go-ldap-client"
-// )
+import(
+	"gopkg.in/korylprince/go-ad-auth.v2"
+	"net/http"
+	"encoding/json"
+)
 
-// func GetUser(login string, password string) bool{
+type User struct{
+	Login string `json:"login"`
+	Password string `json:"password"`
+}
 
-// 	ok, user, err := clientLDAP.Authenticate(login, password)
-// 	if err != nil {
-// 		printError(err)
-// 		return false
-// 	}
-// 	if !ok {
-// 		return false
-// 	}
-// 	return true
-// }
+func LoginRoute(w http.ResponseWriter, r *http.Request){
+	showAPIRequest(r)
+	var userGroups []string
+	Groups := []string{"Студенты", "Персонал", "Бухгалтерия", "Преподаватели", "Админы"}
+	if r.Method == "POST"{
+		config := &auth.Config{
+		Server:   "192.168.10.121",
+        Port:     389,
+        BaseDN:   "DC=ttit,DC=local",
+        Security: auth.SecurityNone,
+		}
 
-// func GetGroupOfUser(login string) []string{
-// 	groups, err := client.GetGroupsOfUser(login)
-// 	if err != nil {
-// 		printError(err)
-// 	}
-// 	return groups
-// }
+		var user User
+		error := json.NewDecoder(r.Body).Decode(&user)
+		if error != nil{
+			json.NewEncoder(w).Encode(struct{ Error string }{Error: "an error has occured during decoding"})
+			showError(r, error)
+			return
+		}
+		status, entry, groups, err := auth.AuthenticateExtended(config, user.Login, user.Password, []string{"cn"}, Groups)
+		if err != nil {
+	   	 	json.NewEncoder(w).Encode(struct{ Error string }{Error: "an error has occured"})
+	   	 	showError(r, err)
+	   	 	return
+		} else if !status {
+    		json.NewEncoder(w).Encode(struct{ Error string }{Error: "no user found"})
+    		return
+		} 
+		for _, group := range groups{
+			for _, constGroup := range Groups{
+				if group == constGroup{
+					userGroups = append(userGroups, group)
+				}
+			}
+		}
+		fmt.Println(entry)
+		json.NewEncoder(w).Encode(userGroups)
+		return
+	}
+}

+ 99 - 98
schedule/xlsx.go

@@ -8,111 +8,112 @@ import (
 )
 
 func ImportXLSX(filename string) []*PlanXLSX {
-    var (
-        listOfPlans []*PlanXLSX
-    )
-    xlFile, err := xlsx.OpenFile(filename)
-    if err != nil {
-        return nil
-    }
-    for _, sheet := range xlFile.Sheets {
-        var plan = new(PlanXLSX)
-        plan.ProgramCode   = sheet.Rows[5].Cells[1].String()
-        plan.NameCode      = strings.Split(sheet.Rows[5].Cells[6].String(), " ")[0]
-        plan.PeriodOfStudy = sheet.Rows[7].Cells[6].String()
-        splited := strings.Split(sheet.Rows[9].Cells[1].String(), " ")
-        plan.GroupNumber   = strings.Split(splited[len(splited)-1], "-")[0]
+    // var (
+    //     listOfPlans []*PlanXLSX
+    // )
+    // xlFile, err := xlsx.OpenFile(filename)
+    // if err != nil {
+    //     return nil
+    // }
+    // for _, sheet := range xlFile.Sheets {
+    //     var plan = new(PlanXLSX)
+    //     plan.ProgramCode   = sheet.Rows[5].Cells[1].String()
+    //     plan.NameCode      = strings.Split(sheet.Rows[5].Cells[6].String(), " ")[0]
+    //     plan.PeriodOfStudy = sheet.Rows[7].Cells[6].String()
+    //     splited := strings.Split(sheet.Rows[9].Cells[1].String(), " ")
+    //     plan.GroupNumber   = strings.Split(splited[len(splited)-1], "-")[0]
 
-        for i, row := range sheet.Rows {
-            if i < 20 {
-                continue
-            }
-            if len(row.Cells) < 20 {
-                continue
-            }
-            if row.Cells[2].String() == "" || row.Cells[2].String() == "ИТОГО" {
-                continue
-            }
+    //     for i, row := range sheet.Rows {
+    //         if i < 20 {
+    //             continue
+    //         }
+    //         if len(row.Cells) < 20 {
+    //             continue
+    //         }
+    //         if row.Cells[2].String() == "" || row.Cells[2].String() == "ИТОГО" {
+    //             continue
+    //         }
 
-            if strings.HasPrefix(row.Cells[1].String(), "Консультации") || strings.HasPrefix(row.Cells[1].String(), "Государственная") {
-                continue
-            }
+    //         if strings.HasPrefix(row.Cells[1].String(), "Консультации") || strings.HasPrefix(row.Cells[1].String(), "Государственная") {
+    //             continue
+    //         }
 
-            splited := strings.Split(row.Cells[1].String(), ".")
-            if len(splited) != 2 {
-                continue
-            }
+    //         splited := strings.Split(row.Cells[1].String(), ".")
+    //         if len(splited) != 2 {
+    //             continue
+    //         }
 
-            typerow := TypeRow(IsSubTR)
-            switch {
-            case strings.Contains(row.Cells[2].String(), "цикл"):
-                typerow = IsCycleTR
-            case splited[1] == "00":
-                typerow = IsStartTR
-            case splited[0] == "ПМ":
-                typerow = IsPmTR
-            }
+    //         typerow := TypeRow(IsSubTR)
+    //         switch {
+    //         case strings.Contains(row.Cells[2].String(), "цикл"):
+    //             typerow = IsCycleTR
+    //         case splited[1] == "00":
+    //             typerow = IsStartTR
+    //         case splited[0] == "ПМ":
+    //             typerow = IsPmTR
+    //         }
 
-            max, _       := row.Cells[4].Int()
-            selfstudy, _ := row.Cells[5].Int()
-            allstudy, _  := row.Cells[6].Int()
-            lectures, _  := row.Cells[7].Int()
-            labs, _      := row.Cells[8].Int()
-            projects, _  := row.Cells[9].Int()
-            c1s1, _      := row.Cells[12].Int()
-            c1s2, _      := row.Cells[13].Int()
-            c2s1, _      := row.Cells[14].Int()
-            c2s2, _      := row.Cells[15].Int()
-            c3s1, _      := row.Cells[16].Int()
-            c3s2, _      := row.Cells[17].Int()
-            c4s1, _      := row.Cells[18].Int()
-            c4s2, _      := row.Cells[19].Int()
+    //         max, _       := row.Cells[4].Int()
+    //         selfstudy, _ := row.Cells[5].Int()
+    //         allstudy, _  := row.Cells[6].Int()
+    //         lectures, _  := row.Cells[7].Int()
+    //         labs, _      := row.Cells[8].Int()
+    //         projects, _  := row.Cells[9].Int()
+    //         c1s1, _      := row.Cells[12].Int()
+    //         c1s2, _      := row.Cells[13].Int()
+    //         c2s1, _      := row.Cells[14].Int()
+    //         c2s2, _      := row.Cells[15].Int()
+    //         c3s1, _      := row.Cells[16].Int()
+    //         c3s2, _      := row.Cells[17].Int()
+    //         c4s1, _      := row.Cells[18].Int()
+    //         c4s2, _      := row.Cells[19].Int()
 
-            certform := ""
-            splited = strings.Split(row.Cells[3].String(), ",")
-            for _, v := range splited {
-                if v != "-" {
-                    certform = v
-                    break
-                }
-            }
+    //         certform := ""
+    //         splited = strings.Split(row.Cells[3].String(), ",")
+    //         for _, v := range splited {
+    //             if v != "-" {
+    //                 certform = v
+    //                 break
+    //             }
+    //         }
 
-            plan.Lines = append(plan.Lines, LineXLSX{
-                ID:        row.Cells[1].String(),
-                Name:      row.Cells[2].String(),
-                CertForm:  certform,
-                StudyLoad: StudyLoadXLSX{
-                    Max:       max,
-                    SelfStudy: selfstudy,
-                    AllStudy:  allstudy,
-                    Lectures:  lectures,
-                    Labs:      labs,
-                    Projects:  projects,
-                },
-                Course: [4][2]int{
-                    [2]int{
-                        c1s1,
-                        c1s2,
-                    },
-                    [2]int{
-                        c2s1,
-                        c2s2,
-                    },
-                    [2]int{
-                        c3s1,
-                        c3s2,
-                    },
-                    [2]int{
-                        c4s1,
-                        c4s2,
-                    },
-                },
-                TypeRow: typerow,
-            })
-        }
-        listOfPlans = append(listOfPlans, plan)
-    }
-    return listOfPlans
+    //         plan.Lines = append(plan.Lines, LineXLSX{
+    //             ID:        row.Cells[1].String(),
+    //             Name:      row.Cells[2].String(),
+    //             CertForm:  certform,
+    //             StudyLoad: StudyLoadXLSX{
+    //                 Max:       max,
+    //                 SelfStudy: selfstudy,
+    //                 AllStudy:  allstudy,
+    //                 Lectures:  lectures,
+    //                 Labs:      labs,
+    //                 Projects:  projects,
+    //             },
+    //             Course: [4][2]int{
+    //                 [2]int{
+    //                     c1s1,
+    //                     c1s2,
+    //                 },
+    //                 [2]int{
+    //                     c2s1,
+    //                     c2s2,
+    //                 },
+    //                 [2]int{
+    //                     c3s1,
+    //                     c3s2,
+    //                 },
+    //                 [2]int{
+    //                     c4s1,
+    //                     c4s2,
+    //                 },
+    //             },
+    //             TypeRow: typerow,
+    //         })
+    //     }
+    //     listOfPlans = append(listOfPlans, plan)
+    // }
+    // return listOfPlans
+    return nil
 }
 
 func CreateXLSX(filename string) (*xlsx.File, string) {