Skip to content
This repository was archived by the owner on May 24, 2024. It is now read-only.

Commit 463da37

Browse files
authored
Merge pull request #22 from LF-Engineering/profile-by-username
Add get identity by username
2 parents 47ca451 + 7dce850 commit 463da37

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

affiliation/dto.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ type UniqueIdentityFullProfile struct {
7979
// Enrollments ...
8080
type Enrollments struct {
8181
Organization *Organization `json:"organization,omitempty"`
82+
End time.Time `json:"end"`
83+
ID int `json:"id"`
84+
Start time.Time `json:"start"`
8285
}
8386

8487
// Organization ...

affiliation/identity.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,80 @@ func (a *Affiliation) GetIdentityByUser(key string, value string) (*AffIdentity,
255255

256256
}
257257

258+
// GetProfileByUsername ...
259+
func (a *Affiliation) GetProfileByUsername(username string, projectSlug string) (*AffIdentity, error) {
260+
if username == "" && projectSlug == "" {
261+
nilKeyOrValueErr := "repository: GetProfileByUsername: username or projectSlug is null"
262+
log.Println(nilKeyOrValueErr)
263+
return nil, fmt.Errorf(nilKeyOrValueErr)
264+
}
265+
266+
token, err := a.auth0Client.ValidateToken(a.Environment)
267+
if err != nil {
268+
log.Println(err)
269+
return nil, err
270+
}
271+
272+
headers := make(map[string]string, 0)
273+
headers["Authorization"] = fmt.Sprintf("%s %s", "Bearer", token)
274+
endpoint := a.AffBaseURL + "/affiliation/" + url.PathEscape(projectSlug) + "/get_profile_by_username/" + url.PathEscape(username)
275+
_, res, err := a.httpClient.Request(strings.TrimSpace(endpoint), "GET", headers, nil, nil)
276+
if err != nil {
277+
log.Println("Repository: GetProfileByUsername: Could not get the profile: ", err)
278+
return nil, err
279+
}
280+
281+
var profile UniqueIdentityFullProfile
282+
err = json.Unmarshal(res, &profile)
283+
if err != nil {
284+
return nil, err
285+
}
286+
var identity AffIdentity
287+
288+
identity.UUID = profile.Identities[0].UUID
289+
identity.Name = *profile.Identities[0].Name
290+
identity.Username = username
291+
identity.Email = *profile.Identities[0].Email
292+
identity.ID = &profile.Identities[0].ID
293+
294+
identity.IsBot = profile.Profile.IsBot
295+
identity.Gender = profile.Profile.Gender
296+
identity.GenderACC = profile.Profile.GenderAcc
297+
298+
if len(profile.Enrollments) > 1 {
299+
identity.OrgName = a.getUserOrg(profile.Enrollments)
300+
for _, org := range profile.Enrollments {
301+
identity.MultiOrgNames = append(identity.MultiOrgNames, org.Organization.Name)
302+
}
303+
} else if len(profile.Enrollments) == 1 {
304+
identity.OrgName = &profile.Enrollments[0].Organization.Name
305+
identity.MultiOrgNames = append(identity.MultiOrgNames, profile.Enrollments[0].Organization.Name)
306+
}
307+
308+
return &identity, nil
309+
}
310+
311+
// Get Most Recent Org Name where user has multiple ennrollments
312+
func (a *Affiliation) getUserOrg(enrollments []*Enrollments) *string {
313+
var result string
314+
var lowest, startTime int64
315+
now := time.Now()
316+
317+
for _, enrollment := range enrollments {
318+
startTime = now.Unix() - enrollment.Start.Unix()
319+
320+
if lowest == 0 {
321+
lowest = startTime
322+
result = enrollment.Organization.Name
323+
} else if startTime < lowest {
324+
lowest = startTime
325+
result = enrollment.Organization.Name
326+
}
327+
}
328+
329+
return &result
330+
}
331+
258332
func buildServices(a *Affiliation) (httpClientProvider *http.ClientProvider, esClientProvider *elastic.ClientProvider, auth0ClientProvider *auth0.ClientProvider, err error) {
259333
esClientProvider, err = elastic.NewClientProvider(&elastic.Params{
260334
URL: a.ESCacheURL,

0 commit comments

Comments
 (0)