Skip to content
This repository has been archived by the owner on Apr 11, 2021. It is now read-only.

Commit

Permalink
Add basic user-auth directives
Browse files Browse the repository at this point in the history
  • Loading branch information
lightster committed Dec 29, 2013
1 parent 8fd374d commit ad013c0
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 22 deletions.
26 changes: 26 additions & 0 deletions public/assets/lidsys/app-models.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
window.ActiveService = class ActiveService
constructor: ->
@user = null


setUser: (@user) ->
getUser: -> @user

isLoggedIn: -> if @user.userId then true else false



window.User = class User
constructor: ->
@userId = null
@permissions = {}


isAuthorized: (permission) ->
if @permissions[permission]? and @permissions[permission] then true else false


setFromApi: (options) ->
@userId = options.user_id
@username = options.username
this
54 changes: 54 additions & 0 deletions public/assets/lidsys/app-models.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 55 additions & 1 deletion public/assets/lidsys/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,63 @@ app.config(['$routeProvider', function ($routeProvider) {
});
}])

app.factory('active', [function() {
return new ActiveService()
}])

app.directive('ldsAuthenticated', ['$rootScope', 'active', function ($rootScope, active) {
return {
restrict: "A",
link: function (scope, element, attrs) {
var wasOriginallyDisplayed = element.css('display')
$rootScope.$watch(
function (scope) {
var expected = (attrs.ldsAuthenticated !== "false")
return expected === active.isLoggedIn()
},
function (isAsExpected, wasAsExpected, scope) {
if (!isAsExpected) {
element.css('display', 'none')
}
else {
element.css('display', wasOriginallyDisplayed)
}
}
)
}
}
}])

app.directive('ldsAuthorized', ['$rootScope', 'active', function ($rootScope, active) {
return {
restrict: "A",
link: function (scope, element, attrs) {
var wasOriginallyDisplayed = element.css('display')
$rootScope.$watch(
function (scope) {
return active.getUser().isAuthorized(attrs.ldsAuthorized)
},
function (isAuthorized, wasAuthorized, scope) {
if (!isAuthorized) {
element.css('display', 'none');
}
else {
element.css('display', wasOriginallyDisplayed)
}
}
)
}
}
}])

app.run(['$rootScope', 'active', function ($rootScope, active) {
active.setUser(new User)
}])

app.controller('AppCtrl', ['$scope', '$location', function ($scope, $location) {
}])

app.controller('LoginCtrl', ['$scope', '$location', '$http', function ($scope, $location, $http) {
app.controller('LoginCtrl', ['$scope', '$location', '$http', 'active', function ($scope, $location, $http, active) {
$scope.formChanged = function ($event) {
var login = $scope.login;

Expand Down Expand Up @@ -63,6 +116,7 @@ app.controller('LoginCtrl', ['$scope', '$location', '$http', function ($scope, $
$http.post("/app/user/login/", postData)
.success(function (data) {
if (data.authenticated_user) {
active.setUser((new User()).setFromApi(data.authenticated_user))
login.error.form = 'Success!!';
}
else {
Expand Down
5 changes: 3 additions & 2 deletions src/Lidsys/Application/Controller/views/index/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<link rel="stylesheet" href="/assets/foundation/css/foundation.css" />
<link rel="stylesheet" href="/assets/lidsys/app.css" />
</head>
<body>
<body ng-controller="AppCtrl">

<nav class="top-bar" data-topbar>
<ul class="title-area">
Expand All @@ -22,7 +22,7 @@ <h1><a href="#/">Lightdatasys</a></h1>

<section class="top-bar-section">
<!-- Right Nav Section -->
<ul class="right">
<ul class="right" lds-authenticated="false">
<li><a href="#/user/login">Login</a></li>
</ul>

Expand All @@ -42,6 +42,7 @@ <h1><a href="#/">Lightdatasys</a></h1>
<script type="text/javascript" src="/assets/angular/angular.js"></script>
<!--<script type="text/javascript" src="assets/angular/angular-cookies.js"></script>-->
<script type="text/javascript" src="/assets/angular/angular-route.js"></script>
<script type="text/javascript" src="/assets/lidsys/app-models.js"></script>
<script type="text/javascript" src="/assets/lidsys/app.js"></script>
<script>
$(document).foundation();
Expand Down
43 changes: 24 additions & 19 deletions src/Lidsys/User/Controller/views/login/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,37 @@
<h2>Login</h2>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<div data-alert class="alert-box warning" ng-show="login.error.form">
{{login.error.form}}
<div lds-authenticated="false">
<div class="row">
<div class="large-12 columns">
<div data-alert class="alert-box warning" ng-show="login.error.form">
{{login.error.form}}
</div>
</div>
</div>
</div>
<div class="row">
<div class="large-12 columns" ng-class="{error: login.error.username}">
<input type="text" placeholder="username" ng-model="login.username" ng-keyup="formChanged($event)" ng-changed="formChanged($event)" />
<small ng-show="login.error.username">{{login.error.username}}</small>
<div class="row">
<div class="large-12 columns" ng-class="{error: login.error.username}">
<input type="text" placeholder="username" ng-model="login.username" ng-keyup="formChanged($event)" ng-changed="formChanged($event)" />
<small ng-show="login.error.username">{{login.error.username}}</small>
</div>
</div>
</div>
<div class="row">
<div class="large-12 columns" ng-class="{error: login.error.password}">
<input type="password" placeholder="password" ng-model="login.password" ng-keyup="formChanged($event)" ng-changed="formChanged($event)" />
<small ng-show="login.error.password">{{login.error.password}}</small>
<div class="row">
<div class="large-12 columns" ng-class="{error: login.error.password}">
<input type="password" placeholder="password" ng-model="login.password" ng-keyup="formChanged($event)" ng-changed="formChanged($event)" />
<small ng-show="login.error.password">{{login.error.password}}</small>
</div>
</div>
</div>
<div class="row">
<div class="large-5 columns">
<input type="submit" class="button small" value="Sign In" />
<div class="row">
<div class="large-5 columns">
<input type="submit" class="button small" value="Sign In" />
</div>
</div>
</div>
<div data-alert class="large-12 columns alert-box warning" lds-authenticated="true">
You are already logged in!
</div>
</div>
<div class="large-5 medium-4 columns">
</div>
</div>
</form>
</form>
1 change: 1 addition & 0 deletions src/Lidsys/User/Service/AuthenticatorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function getUserForUsernameAndPassword($username, $password)
$query = $pdo->prepare(
"
SELECT
u.userId AS user_id,
u.username,
u.timeZone AS time_zone
FROM user AS u
Expand Down

0 comments on commit ad013c0

Please sign in to comment.