ref (http://docs.mongodb.org/manual/reference/method/js-user-management/)
/*
* db.createUser()
*/
// products db에 nspark유저를 생성.
// admin db에 readAnyDatabase와 clusterAdmin role을 부여
// products db는 readWrite role을 부여
use products
db.createUser( { "user" : "nspark",
"pwd": "password string",
"customData" : { employeeId: 12345 },
"roles" : [ { role: "clusterAdmin", db: "admin" },
{ role: "readAnyDatabase", db: "admin" },
"readWrite"
] },
{ w: "majority" , wtimeout: 5000 } )
// products db에 dbAccount1유저를 생성하고 readWrite role을 부여
use products
db.createUser(
{
user: "dbAccount1",
pwd: "password",
roles: [ "readWrite", "dbAdmin" ]
}
)
// admin db에 role없이 reportsUser유저를 생성
use admin
db.createUser(
{
user: "reportsUser",
pwd: "password",
roles: [ ]
}
)
// admin db에 clusterAdmin role을 가진 appAdmin유저를 생성
// config db에 readWrite role을 생성
use admin
db.createUser(
{
user: "appAdmin",
pwd: "password",
roles:
[
{ role: "readWrite", db: "config" },
"clusterAdmin"
]
}
)
db.changeUserPassword("reporting", "SOh3TbYhx8ypJPxmt1oOfL");
// 사용자 정의 role 생성
// ____________________________________________________________________________
use admin
db.createRole({
// 사용자 정의 role 이름
role: "userRole1",
// 권한 리스트
privileges: [
// product.inventory collection은 읽기/쓰기 가능 (삭제는 못함)
{
resource: {db: "product", collection: "inventory"},
actions: ["find", "update", "insert"]
},
// product.orders collection은 read만 가능
{
resource: {db: "product", collection: "orders"},
actions: ["find"]
}
],
// built-in-roles에서 상속받고자 하는 role을 기입. 필수값이므로 없으면 빈 array라도 있어야 함.
roles: []
})
// 생성된 권한을 특정 사용자에게 부여
// ____________________________________________________________________________
db.createUser({user: "user1", pwd: "1111", roles: ["userRole1"]})
// 권한이 부여된 사용자를 이용하여 테스트
// ____________________________________________________________________________
use product
// 성공
db.inventory.insert({no: 1, name: "product1"})
// 실패
db.orders.insert({no: 1, products: [1,2,3]})
// 성공
db.inventory.find()
// 성공
db.orders.find()