Advertisement
Guest User

Untitled

a guest
Dec 12th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. public function createAction(Request $request){
  2. $em = $this->getDoctrine()->getManager();
  3. $post_send = $request->request->get('userbundle_user');
  4. if (array_key_exists('id', $post_send)) {
  5. $ldap_success = true;
  6. $entity = $em->getRepository('UserBundle:User')->find($post_send['id']);
  7. }else{
  8. $ldapconn = ldap_connect("10.0.0.230")
  9. or die("Could not connect to LDAP server.");
  10.  
  11. $ldap_success = false;
  12. if ($ldapconn) {
  13. try{
  14. $ldapbind = ldap_bind($ldapconn, $post_send['username'], $post_send['password']);
  15.  
  16. //Now I have to check if the user exist first on the Active Directory
  17. //$ldaptree = "OU=SBSUsers,OU=Users,OU=MyBusiness,DC=myDomain,DC=local" I'm not really sure what should I put here;
  18. //$ldapsearch = ldap_search($ldapconn,$ldaptree, $post_send['username']) or die ("Error in search query: ".ldap_error($ldapconn));+
  19.  
  20. if ($ldapbind) {
  21. $ldap_success = true;
  22. } else {
  23. $ldap_success = false;
  24. }
  25. }
  26. catch(Exception $e){
  27. $ldap_success = false;
  28. }
  29. }
  30. $entity = new User();
  31. }
  32.  
  33. if( $ldap_success && $ldapsearch){ //Here I expect both true, the success conecction and the search, only with both true I will create a user.
  34. $entity->setUsername($post_send['username']);
  35. $entity->setRut($post_send['rut']);
  36. if (array_key_exists('password', $post_send)) {
  37. if ( $post_send['password'] != "" ) {
  38. $entity->setPassword($post_send['password']);
  39. $this->setSecurePassword($entity);
  40. }
  41. }
  42. $entity->setEmail($post_send['email']);
  43. $entity->setDateAdded(new DateTime());
  44. $entity->setIsActive(true);
  45. $entity->setIsAdmin(true);
  46. $entity->setUserRoles($em->getRepository('UserBundle:Role')->find( $post_send['admin_roles_id'] ));
  47. $entity->setWizardCompleted(true);
  48. $em->persist($entity);
  49. $em->flush();
  50. $json = new JsonUtils();
  51. return $json->arrayToJson(array("id"=>$entity->getId()));
  52. }
  53. return $json->arrayToJson( array("sueccess"=>false ) );
  54. }
  55.  
  56. $ldapconn = ldap_connect("10.0.0.230");
  57. ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
  58. ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0)
  59. // Use a LDAP service account with only read access...
  60. $searchUser = 'user@myDomain.com';
  61. $searchPass = '12345';
  62.  
  63. $ldap_success = false;
  64. if (@ldap_bind($ldapconn, $searchUser, $searchPass)) {
  65. $attributes = ['cn'];
  66. $filter = "(&(objectClass=user)(objectCategory=person)(userPrincipalName=".ldap_escape($post_send['username'], null, LDAP_ESCAPE_FILTER)."))";
  67. $baseDn = "DC=myDomain,DC=com";
  68. $results = @ldap_search($ldapconn, $baseDn, $filter, $attributes);
  69. $info = @ldap_get_entries($ldapconn, $results);
  70.  
  71. $ldap_success = ($info && $info['count'] === 1);
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement