44 "fmt"
55 "strings"
66
7+ "github.com/opensourceways/app-cla-server/config"
78 "github.com/opensourceways/app-cla-server/dbmodels"
89 "github.com/opensourceways/app-cla-server/util"
910)
@@ -14,7 +15,7 @@ type CorporationManagerAuthentication struct {
1415}
1516
1617func (this CorporationManagerAuthentication ) Authenticate () (map [string ]dbmodels.CorporationManagerCheckResult , IModelError ) {
17- info := dbmodels.CorporationManagerCheckInfo {Password : this . Password }
18+ info := dbmodels.CorporationManagerCheckInfo {}
1819 if merr := checkEmailFormat (this .User ); merr == nil {
1920 info .Email = this .User
2021 } else {
@@ -29,25 +30,35 @@ func (this CorporationManagerAuthentication) Authenticate() (map[string]dbmodels
2930
3031 v , err := dbmodels .GetDB ().CheckCorporationManagerExist (info )
3132 if err == nil {
33+ for k := range v {
34+ if ! isSamePasswords (v [k ].Password , this .Password ) {
35+ delete (v , k )
36+ }
37+ }
3238 return v , nil
3339 }
3440
3541 return nil , parseDBError (err )
3642}
3743
3844func CreateCorporationAdministrator (linkID , name , email string ) (* dbmodels.CorporationManagerCreateOption , IModelError ) {
39- pw := util .RandStr (8 , "alphanum" )
45+ pw := newPWForCorpManager ()
46+ encryptedPW , merr := encryptPassword (pw )
47+ if merr != nil {
48+ return nil , merr
49+ }
4050
4151 opt := & dbmodels.CorporationManagerCreateOption {
4252 ID : "admin" ,
4353 Name : name ,
4454 Email : email ,
45- Password : pw ,
55+ Password : encryptedPW ,
4656 Role : dbmodels .RoleAdmin ,
4757 }
4858 err := dbmodels .GetDB ().AddCorpAdministrator (linkID , opt )
4959 if err == nil {
5060 opt .ID = fmt .Sprintf ("admin_%s" , util .EmailSuffix (email ))
61+ opt .Password = pw
5162 return opt , nil
5263 }
5364
@@ -64,23 +75,67 @@ func (this CorporationManagerResetPassword) Validate() IModelError {
6475 if this .NewPassword == this .OldPassword {
6576 return newModelError (ErrSamePassword , fmt .Errorf ("the new password is same as old one" ))
6677 }
67- return nil
78+
79+ n := len (this .NewPassword )
80+ cfg := config .AppConfig
81+ if n < cfg .MinLengthOfPassword || n > cfg .MaxLengthOfPassword {
82+ return newModelError (
83+ ErrTooShortOrLongPassword ,
84+ fmt .Errorf (
85+ "the length of password should be between %d and %d" ,
86+ cfg .MinLengthOfPassword , cfg .MaxLengthOfPassword ,
87+ ))
88+ }
89+
90+ return checkPassword (this .NewPassword )
6891}
6992
7093func (this CorporationManagerResetPassword ) Reset (linkID , email string ) IModelError {
94+ pw , merr := encryptPassword (this .NewPassword )
95+ if merr != nil {
96+ return merr
97+ }
98+
99+ record , merr := this .getCorporationManager (linkID , email )
100+ if merr != nil {
101+ return merr
102+ }
103+ if record == nil {
104+ return newModelError (ErrCorpManagerDoesNotExist , fmt .Errorf ("corp manager does not exist" ))
105+ }
106+
107+ if ! isSamePasswords (record .Password , this .OldPassword ) {
108+ return newModelError (ErrWrongOldPassword , fmt .Errorf ("old password is not correct" ))
109+ }
110+
71111 err := dbmodels .GetDB ().ResetCorporationManagerPassword (
72- linkID , email , dbmodels .CorporationManagerResetPassword (this ),
112+ linkID , email , dbmodels.CorporationManagerResetPassword {
113+ OldPassword : record .Password , NewPassword : pw ,
114+ },
73115 )
74116 if err == nil {
75117 return nil
76118 }
77119
78120 if err .IsErrorOf (dbmodels .ErrNoDBRecord ) {
79- return newModelError (ErrNoLinkOrNoManager , err )
121+ return newModelError (ErrNoLinkOrNoManagerOrFO , err )
80122 }
81123 return parseDBError (err )
82124}
83125
126+ func (this CorporationManagerResetPassword ) getCorporationManager (linkID , email string ) (* dbmodels.CorporationManagerCheckResult , IModelError ) {
127+ v , err := dbmodels .GetDB ().GetCorporationManager (linkID , email )
128+ if err == nil {
129+ return v , nil
130+ }
131+
132+ if err .IsErrorOf (dbmodels .ErrNoDBRecord ) {
133+ return v , newModelError (ErrNoLink , err )
134+ }
135+
136+ return v , parseDBError (err )
137+ }
138+
84139func ListCorporationManagers (linkID , email , role string ) ([]dbmodels.CorporationManagerListResult , IModelError ) {
85140 v , err := dbmodels .GetDB ().ListCorporationManager (linkID , email , role )
86141 if err == nil {
@@ -96,3 +151,7 @@ func ListCorporationManagers(linkID, email, role string) ([]dbmodels.Corporation
96151
97152 return v , parseDBError (err )
98153}
154+
155+ func newPWForCorpManager () string {
156+ return util .RandStr (8 , "alphanum" )
157+ }
0 commit comments