// validates the user against the LDAP System and returns its propertues as a Dictionary public IActionResult CheckUserInLdapAndReturnAttributes(string username, string password) { var validCredentials = LdapHelper.ValidateCredentials(username, password); if (!validCredentials) { return(new BadRequestObjectResult("Wrong credentials")); } var ldapUserAttributes = LdapHelper.GetLdapUser(username); if (ldapUserAttributes == null) { return(new NotFoundObjectResult("User Not Found in LDAP")); } if (CheckIfUserAllreadyRegistered(ldapUserAttributes)) { return(new BadRequestObjectResult("User Allready Registered")); } return(new OkObjectResult(ldapUserAttributes)); }
// creates a new user public IActionResult CreateUser(string rzName, string rzPassword, UserCreateDto user) { var rzNameData = Convert.FromBase64String(rzName); var decodedRzname = Encoding.UTF8.GetString(rzNameData); var rzPasswordData = Convert.FromBase64String(rzPassword); var decodedPassword = Encoding.UTF8.GetString(rzPasswordData); var ldapCredentialsAreValid = CheckIfLdapCredentialsAreValid(decodedRzname, decodedPassword); if (!ldapCredentialsAreValid) { return(new BadRequestObjectResult("Wrong Ldap Credentials")); } var ldapUserAsDictionary = LdapHelper.GetLdapUser(decodedRzname); var ldapIdOfUser = Convert.ToInt32(ldapUserAsDictionary["uidNumber"]); var userAllreadyExists = CheckIfUserAllreadyExists(ldapIdOfUser, decodedRzname); if (userAllreadyExists) { return(new BadRequestObjectResult("User is allready registered")); } var newUser = user.ToModel(); newUser.Firstname = ldapUserAsDictionary["givenName"]; newUser.Lastname = ldapUserAsDictionary["sn"]; newUser.EmployeeType = ldapUserAsDictionary["employeeType"]; newUser.RzName = ldapUserAsDictionary["uid"]; newUser.LdapId = Convert.ToInt32(ldapUserAsDictionary["uidNumber"]); _applicationDbContext.AppUser.Add(newUser); Save(); return(new CreatedResult($"/users/{newUser.Id}", newUser.ToDetailDto())); }