1414use Symfony \Component \Ldap \Adapter \EntryManagerInterface ;
1515use Symfony \Component \Ldap \Entry ;
1616use Symfony \Component \Ldap \Exception \LdapException ;
17+ use Symfony \Component \Ldap \Exception \NotBoundException ;
1718
1819/**
1920 * @author Charles Sarrazin <charles@sarraz.in>
21+ * @author Bob van de Vijver <bobvandevijver@hotmail.com>
2022 */
2123class EntryManager implements EntryManagerInterface
2224{
@@ -32,7 +34,7 @@ public function __construct(Connection $connection)
3234 */
3335 public function add (Entry $ entry )
3436 {
35- $ con = $ this ->connection -> getResource ();
37+ $ con = $ this ->getConnectionResource ();
3638
3739 if (!@ldap_add ($ con , $ entry ->getDn (), $ entry ->getAttributes ())) {
3840 throw new LdapException (sprintf ('Could not add entry "%s": %s ' , $ entry ->getDn (), ldap_error ($ con )));
@@ -46,7 +48,7 @@ public function add(Entry $entry)
4648 */
4749 public function update (Entry $ entry )
4850 {
49- $ con = $ this ->connection -> getResource ();
51+ $ con = $ this ->getConnectionResource ();
5052
5153 if (!@ldap_modify ($ con , $ entry ->getDn (), $ entry ->getAttributes ())) {
5254 throw new LdapException (sprintf ('Could not update entry "%s": %s ' , $ entry ->getDn (), ldap_error ($ con )));
@@ -58,10 +60,23 @@ public function update(Entry $entry)
5860 */
5961 public function remove (Entry $ entry )
6062 {
61- $ con = $ this ->connection -> getResource ();
63+ $ con = $ this ->getConnectionResource ();
6264
6365 if (!@ldap_delete ($ con , $ entry ->getDn ())) {
6466 throw new LdapException (sprintf ('Could not remove entry "%s": %s ' , $ entry ->getDn (), ldap_error ($ con )));
6567 }
6668 }
69+
70+ /**
71+ * Get the connection resource, but first check if the connection is bound.
72+ */
73+ private function getConnectionResource ()
74+ {
75+ // If the connection is not bound, throw an exception. Users should use an explicit bind call first.
76+ if (!$ this ->connection ->isBound ()) {
77+ throw new NotBoundException ('Query execution is not possible without binding the connection first. ' );
78+ }
79+
80+ return $ this ->connection ->getResource ();
81+ }
6782}
0 commit comments