Code Monkey home page Code Monkey logo

cozy-clearance's Introduction

cozy-clearance

Helper package to manage clearances in cozy. This package include two parts:

  • server side
  • client side

Usage : Server Side

Base Module

documentation

clearance = require 'cozy-clearance'

details  = {email:"[email protected]", contactid:"3615", any:"other field"}
details2 = {email:"[email protected]", contactid:"3616", any:"other field"}

The clearance.add function allows you to add a rule to a model

clearance.add someModel, 'rw', details, (err) ->
    clearance.add someModel, 'r', details2, (err) ->
        console.log someModel.clearance
# [
#     {email:"[email protected]", contactid:"3615", any:"other field", key:"secret", perm:"rw"}
#     {email:"[email protected]", contactid:"3616", any:"other field", key:"secret2", perm:"rw"}
# ]

The clearance.check function allows you to check a request against the model. It looks for the key in the request's querystring The callback is called with the matching rule if found, false otherwise

req.query.key = "secret"
clearance.check someModel, 'r', req, (err, rule) ->
    # rule == {email:"[email protected]", contactid:"3615", any:"other field", key:"secret", perm:"rw"}

clearance.check someModel, 'w', req, (err, rule) ->
    # rule == false, steve doesn't have the 'w' permission

The clearance.revoke function allows you to revoke a rule for the model. All rules matching the given object will be revoked

clearance.revoke someModel, {email:"[email protected]"}, (err) ->
    console.log someModel.clearance
    # [{email:"[email protected]", contactid:"3616", any:"other field", key:"secret2", perm:"rw"}]

# or

clearance.revoke someModel, {any:"other field"}, (err) ->
    console.log someModel.clearance
    # []

Controller :

To use the client side of cozy-clearance, you will need to expose some of the controller's routes.

# in routes.coffee
clearance = require 'cozy-clearance'

# use mailSubject & mailTemplate functions to customize the sent mail.
clearanceCtl = clearance.controller
    mailSubject: (options) -> # options.doc , options.url
    mailTemplate: (options) -> # options.doc , options.url

'docid':
    param: # fetch and save in req.doc
'clearance/contacts':
    get: clearanceCtl.contactList
'clearance/contacts/:contactid':
    get: clearanceCtl.contact
'clearance/contacts/:contactid.jpg':
    get: sharing.contactPicture
'clearance/:docid'
    put: clearanceCtl.change
'clearance/:docid/send':
    post: clearanceCtl.sendAll

Usage : Client Side

Your client side environement should include the following :

  • a global require & require.define, following the commonjs convention (like brunch)
  • a global t function that handles translations

Include the file client-build.js or client-build.min.js in your vendor/scripts folder and use it like this :

CozyClearanceModal = require 'cozy-clearance/modal_share_view'
new CozyClearanceModal model: someModel

You can override some methods :

class YourModalView extends CozyClearanceModal

    # change the permissions method to add possible permissions
    permissions: ->
        'r': 'see this'
        'rw': 'see and edit'
        'rwy': 'see, edit and do Y'
        'rwz': 'see, edit and do Z'
        # note : list all possible combinations, here, you can't have both Y & Z permissions

See cozy-files for heavy customization.

The contactCollection can be plugged to your app's realtime :

contactCollection = require 'cozy-clearance/contact_collection'
socketListener.process = (event) ->
   {doctype, operation, id} = event
   # ...
   contactCollection.handleRealtimeContactEvent event

## Locales

The client-side library use the following locale keys:
 - "cancel"
 - "confirm"
 - "copy paste link"
 - "mail not send"
 - "modal error"
 - "modal ok"
 - "modal question " + type + " shareable"
 - "modal send mails
 - "modal shared " + type + " custom msg"
 - "modal shared public link msg"
 - "modal shared with people msg"
 - "no forgot"
 - "no"
 - "only you can see"
 - "perm"
 - "private"
 - "r"
 - "revoke"
 - "save"
 - "see link"
 - "send email hint"
 - "send mails question"
 - "server error occured"
 - "share confirm save"
 - "share forgot add"
 - "shared"
 - "sharing"
 - "yes forgot"
 - "yes"

## Contribute

Use [coffeegulp](https://github.com/minibikini/coffeegulp) to build the client
side.

Use npm run build to build the server side

use npm test to run tests

cozy-clearance's People

Contributors

aenario avatar clochix avatar jsilvestre avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.