-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from Nuvindu/examples
- Loading branch information
Showing
17 changed files
with
379 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../access-directory-server.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
org = "ballerina" | ||
name = "access_directory_server" | ||
version = "0.1.0" | ||
distribution = "2201.8.0" | ||
|
||
[build-options] | ||
observabilityIncluded = true |
30 changes: 30 additions & 0 deletions
30
examples/access-directory-server/access-directory-server.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Employee Directory and Authentication System | ||
|
||
This example demonstrates using the Ballerina LDAP module to integrate with a directory server to manage employees in a corporation. The functionalities include authenticating with the directory server, adding a new user, searching for users, updating user details, and deleting a user. | ||
|
||
## Running an Example | ||
|
||
### 1. Start the directory server | ||
|
||
Run the following docker command to start the LDAP server. | ||
|
||
```sh | ||
cd resources | ||
docker compose up | ||
``` | ||
|
||
### 2. Run the Ballerina project | ||
|
||
Execute the following commands to build an example from the source: | ||
|
||
* To build an example: | ||
|
||
```bash | ||
bal build | ||
``` | ||
|
||
* To run an example: | ||
|
||
```bash | ||
bal run | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) 2024 WSO2 LLC. (http://www.wso2.com). | ||
// | ||
// WSO2 LLC. licenses this file to you under the Apache License, | ||
// Version 2.0 (the "License"); you may not use this file except | ||
// in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
import ballerina/ldap; | ||
import ballerina/io; | ||
|
||
configurable string hostName = ?; | ||
configurable int port = ?; | ||
configurable string domainName = ?; | ||
configurable string password = ?; | ||
configurable string userDN = ?; | ||
|
||
type Employee record { | ||
string[] objectClass; | ||
string sn; | ||
}; | ||
|
||
public function main() returns error? { | ||
ldap:Client ldapClient = check new ({ | ||
hostName, | ||
port, | ||
domainName, | ||
password | ||
}); | ||
|
||
ldap:Entry employee = { | ||
"objectClass": ["top", "person"], | ||
"sn": "New User" | ||
}; | ||
|
||
// Add a new employee to the directory server. | ||
ldap:LdapResponse addResponse = check ldapClient->add("cn=User,dc=mycompany,dc=com", employee); | ||
io:println(addResponse); | ||
|
||
// Search for the employee. | ||
Employee[] result = check ldapClient->searchWithType("dc=mycompany,dc=com", "(sn=New User)", ldap:SUB); | ||
io:println(result); | ||
|
||
// Update the employee. | ||
ldap:LdapResponse updateResponse = check ldapClient->modify("cn=User,dc=mycompany,dc=com", { "sn": "Updated User" }); | ||
io:println(updateResponse); | ||
|
||
// Delete the employee. | ||
ldap:LdapResponse response = check ldapClient->delete("CN=User,dc=mycompany,dc=com"); | ||
io:println(response); | ||
} |
14 changes: 14 additions & 0 deletions
14
examples/access-directory-server/resources/docker-compose.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
version: '3.7' | ||
|
||
services: | ||
ldap_server: | ||
image: osixia/openldap:latest | ||
container_name: my-openldap-container | ||
environment: | ||
LDAP_ORGANISATION: "My Company" | ||
LDAP_DOMAIN: "mycompany.com" | ||
LDAP_ADMIN_PASSWORD: "adminpassword" | ||
ports: | ||
- "389:389" | ||
- "636:636" | ||
command: --copy-service |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com) | ||
* | ||
* WSO2 LLC. licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import org.apache.tools.ant.taskdefs.condition.Os | ||
|
||
description = 'Ballerina - LDAP Examples' | ||
|
||
def ballerinaDist = "${project.rootDir}/target/ballerina-runtime" | ||
def examples = ["access-directory-server"] | ||
def graalvmFlag = "" | ||
|
||
tasks.register('clean') { | ||
examples.forEach { example -> | ||
delete "${projectDir}/${example}/target" | ||
delete "${projectDir}/${example}/Dependencies.toml" | ||
} | ||
} | ||
|
||
task testExamples { | ||
if (project.hasProperty("balGraalVMTest")) { | ||
graalvmFlag = "--graalvm" | ||
} | ||
doLast { | ||
examples.each { example -> | ||
try { | ||
exec { | ||
workingDir project.projectDir | ||
if (Os.isFamily(Os.FAMILY_WINDOWS)) { | ||
commandLine 'cmd', '/c', "${ballerinaDist}/bin/bal.bat test ${graalvmFlag} ${example} && exit %%ERRORLEVEL%%" | ||
} else { | ||
commandLine 'sh', '-c', "${ballerinaDist}/bin/bal test ${graalvmFlag} ${example}" | ||
} | ||
} | ||
} catch (Exception e) { | ||
println("Example '${example}' Build failed: " + e.message) | ||
throw e | ||
} | ||
} | ||
} | ||
} | ||
|
||
task buildExamples { | ||
gradle.taskGraph.whenReady { graph -> | ||
if (graph.hasTask(":ldap-examples:test")) { | ||
buildExamples.enabled = false | ||
} else { | ||
testExamples.enabled = false | ||
} | ||
} | ||
doLast { | ||
examples.each { example -> | ||
try { | ||
exec { | ||
workingDir project.projectDir | ||
if (Os.isFamily(Os.FAMILY_WINDOWS)) { | ||
commandLine 'cmd', '/c', "${ballerinaDist}/bin/bal.bat build ${example} && exit %%ERRORLEVEL%%" | ||
} else { | ||
commandLine 'sh', '-c', "${ballerinaDist}/bin/bal build ${example}" | ||
} | ||
} | ||
} catch (Exception e) { | ||
println("Example '${example}' Build failed: " + e.message) | ||
throw e | ||
} | ||
} | ||
} | ||
} | ||
|
||
task build { | ||
dependsOn buildExamples | ||
|
||
} | ||
|
||
task test { | ||
dependsOn testExamples | ||
} | ||
|
||
buildExamples.dependsOn ":ldap-ballerina:build" | ||
testExamples.dependsOn ":ldap-ballerina:build" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../library-managment-system.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[package] | ||
org = "ballerina" | ||
name = "library_managment_system" | ||
version = "0.1.0" | ||
distribution = "2201.8.0" |
30 changes: 30 additions & 0 deletions
30
examples/library-managment-system/library-managment-system.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Employee Directory and Authentication System | ||
|
||
This example demonstrates using the Ballerina LDAP module to integrate with a directory server for managing users in a library system. The functionalities include adding new users, searching for books, and updating books. | ||
|
||
## Running an Example | ||
|
||
### 1. Start the directory server | ||
|
||
Run the following docker command to start the LDAP server. | ||
|
||
```sh | ||
cd resources | ||
docker-compose up | ||
``` | ||
|
||
### 2. Run the Ballerina project | ||
|
||
Execute the following commands to build an example from the source: | ||
|
||
* To build an example: | ||
|
||
```bash | ||
bal build | ||
``` | ||
|
||
* To run an example: | ||
|
||
```bash | ||
bal run | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) 2024 WSO2 LLC. (http://www.wso2.com). | ||
// | ||
// WSO2 LLC. licenses this file to you under the Apache License, | ||
// Version 2.0 (the "License"); you may not use this file except | ||
// in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
import ballerina/ldap; | ||
import ballerina/io; | ||
|
||
configurable string hostName = ?; | ||
configurable int port = ?; | ||
configurable string domainName = ?; | ||
configurable string password = ?; | ||
|
||
public function main() returns error? { | ||
ldap:Client ldapClient = check new ({ | ||
hostName, | ||
port, | ||
domainName, | ||
password | ||
}); | ||
|
||
ldap:Entry user = { | ||
"objectClass": "inetOrgPerson", | ||
"sn": "Alice", | ||
"cn": "Alice", | ||
"uid": "alice", | ||
"displayName": "Alice Parker" | ||
}; | ||
|
||
// Add a new user to the library system. | ||
_ = check ldapClient->add("uid=alice,ou=Users,dc=library,dc=org", user); | ||
|
||
// Search for a book. | ||
ldap:SearchResult result = check ldapClient->search("ou=Books,dc=library,dc=org", "(cn=Dracula)", ldap:SUB); | ||
io:println(result.entries); | ||
|
||
ldap:Entry updateBook = { | ||
"member": "uid=alice,ou=Users,dc=library,dc=org" | ||
}; | ||
|
||
// Updates the book. | ||
_ = check ldapClient->modify("cn=Dracula,ou=Books,dc=library,dc=org", updateBook); | ||
|
||
result = check ldapClient->search("ou=Books,dc=library,dc=org", "(cn=Dracula)", ldap:SUB); | ||
io:println(result.entries); | ||
} |
43 changes: 43 additions & 0 deletions
43
examples/library-managment-system/resources/bootstrap.ldif
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
dn: ou=Users,dc=library,dc=org | ||
changetype: add | ||
objectClass: organizationalUnit | ||
ou: Users | ||
|
||
dn: ou=Groups,dc=library,dc=org | ||
changetype: add | ||
objectClass: organizationalUnit | ||
ou: Groups | ||
|
||
dn: ou=Books,dc=library,dc=org | ||
changetype: add | ||
objectClass: organizationalUnit | ||
ou: Books | ||
|
||
dn: uid=john,ou=Users,dc=library,dc=org | ||
changetype: add | ||
objectClass: inetOrgPerson | ||
cn: John | ||
givenName: John | ||
sn: John | ||
uid: john | ||
displayName: John Doe | ||
mail: [email protected] | ||
userPassword: johndoe@123 | ||
|
||
dn: cn=Users,ou=Groups,dc=library,dc=org | ||
changetype: add | ||
cn: Users | ||
objectClass: groupOfNames | ||
member: uid=john,ou=Users,dc=library,dc=org | ||
|
||
dn: cn=Books,ou=Groups,dc=library,dc=org | ||
changetype: add | ||
cn: Books | ||
objectClass: groupOfNames | ||
member: uid=john,ou=Users,dc=library,dc=org | ||
|
||
dn: cn=Dracula,ou=Books,dc=library,dc=org | ||
changetype: add | ||
objectClass: groupOfNames | ||
cn: Dracula | ||
member: uid=john,ou=Users,dc=library,dc=org |
16 changes: 16 additions & 0 deletions
16
examples/library-managment-system/resources/docker-compose.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
version: '3.7' | ||
|
||
services: | ||
ldap_server: | ||
image: osixia/openldap:latest | ||
container_name: my-openldap-container | ||
environment: | ||
LDAP_ORGANISATION: "Library" | ||
LDAP_DOMAIN: "library.org" | ||
LDAP_ADMIN_PASSWORD: "adminpassword" | ||
ports: | ||
- "389:389" | ||
- "636:636" | ||
command: --copy-service | ||
volumes: | ||
- ./bootstrap.ldif:/container/service/slapd/assets/config/bootstrap/ldif/50-bootstrap.ldif |
Oops, something went wrong.