From 51ccc779192bbb31c864ccc98ff9580854f60b76 Mon Sep 17 00:00:00 2001 From: Michael Foiani Date: Wed, 1 Aug 2018 17:29:57 -0400 Subject: Working on admin framework. Allows for certain uids to have extra controls if they use a key command and enter a password pulled from a database. --- src/actions/firebase.js | 82 ++++++++++++++++++++++++++++++++++++++++-- src/components/mao-account.js | 72 +++++++++++++++++++++++++++++++------ src/components/mao-app.js | 1 - src/components/mao-fourms.js | 4 +-- src/components/mao-tutoring.js | 22 ++++++++---- src/reducers/firebase.js | 12 +++++-- 6 files changed, 167 insertions(+), 26 deletions(-) diff --git a/src/actions/firebase.js b/src/actions/firebase.js index a9da36a..7c4a4ad 100644 --- a/src/actions/firebase.js +++ b/src/actions/firebase.js @@ -4,9 +4,25 @@ import { firebase, firestore } from '../firebase.js'; export const AUTH_FAIL = 'AUTH_FAIL'; export const AUTH_SUCCESS = 'AUTH_SUCCESS'; export const AUTH_SIGN_OUT = 'AUTH_SIGN_OUT'; +export const CREATE_ACCOUNT = 'CREATE_ACCOUNT'; +export const ADMIN_LISTENER = 'ADMIN_LISTENER'; +export const IS_ADMIN = 'IS_ADMIN'; +export const ADMIN_CONTROLS = 'ADMIN_CONTROLS'; +export const UPDATE_ADMIN = 'UPDATE_ADMIN'; const auth = firebase.auth(); +export const createAccount = (_email, _password) => (dispatch) => { + var authTrue = false; + firebase.auth().createUserWithEmailAndPassword(_email, _password).then(() => { + dispatch(signIn(_email, _password)); + }) + .catch((error) => { + // Handle Errors here. + alert(error.code + ": " + error.message); + }); +} + export const signIn = (_email, _password) => (dispatch) => { auth.signInWithEmailAndPassword(_email, _password).then(() => { var user = auth.currentUser; @@ -22,6 +38,13 @@ export const signIn = (_email, _password) => (dispatch) => { dispatch(authSuccess(user)); dispatch(snapshotHours()) dispatch(snapshotRegisteredCompetitions()); + //Admin controls + if( user.uid === 'rxKROQAukzchWuueDLwA9c0YmsT2' || //Lucy Wood + user.uid === 'sAVjlnSAETaP5VtTKGhfBKHKeQF2' //Michael Foiani + ) + { + dispatch(adminListener()); + } }) .catch((error) => { dispatch(authFail(error.code)); @@ -29,6 +52,19 @@ export const signIn = (_email, _password) => (dispatch) => { } +export const adminListener = () => (dispatch, getState) => { + document.onkeyup = function(e) { + if(e.altKey && e.which == 65) { + var docRef = firestore.collection('keys').doc('adminKey'); + docRef.get().then((doc) => { + if(prompt('Enter admin password') == doc.data().password) { + dispatch(adminControls()); + } + }); + } + } +} + export const authFail = (errorCode) => { alert(errorCode); return { @@ -50,6 +86,17 @@ export const authSuccess = (_user) => { } } +export const adminControls = () => (dispatch) => { + dispatch(updateAdmin()); +} + +export const updateAdmin = () => { + return { + type: UPDATE_ADMIN, + payload: true + } +} + export const signOut = () => (dispatch) => { auth.signOut().then(() => { dispatch(authSignOut()); @@ -62,7 +109,10 @@ export const authSignOut = () => { payload: false, code: "Signed Out User", uid: "", - userEmail: "" + userEmail: "", + isAdmin: false, + requests: [], + compList: [] } } //End Firebase Auth @@ -107,6 +157,8 @@ export const updateHours = (hours, reqHours) => { export const REGISTER_COMP = 'REGISTER_COMP'; export const SNAPSHOT_REGISTERED_COMPETITIONS = 'SNAPSHOT_REGISTERED_COMPETITIONS'; export const UPDATE_REGISTERED_COMPETITIONS = 'UPDATE_REGISTERED_COMPETITIONS'; +export const FETCH_ALL_REQUESTS = 'FETCH_ALL_REQUESTS'; +export const FETCH_ADMIN_REQUESTS = 'FETCH_ADMIN_REQUESTS'; export const registerComp = (compName) => (dispatch, getState) => { var docRef = firestore.collection('competitions').doc(compName); @@ -149,7 +201,28 @@ export const updateRegisteredCompetitions = (registeredComps) => { } } -export const REQUEST_HOURS = 'REQUEST_HOURS'; +export const fetchAllRequests = () => (dispatch) => { + var docRef = firestore.collection('requests'); + + docRef.onSnapshot((query) => { + var requests = []; + query.forEach((doc) => { + requests.push(doc.data()); + }); + console.log(requests); + dispatch(fetchAdminRequests(requests)); + }); +} + +export const fetchAdminRequests = (requests) => { + return { + type: FETCH_ADMIN_REQUESTS, + payload: requests + } +} + +export const REQUEST_HOURS = 'REQUEST_HOURS'; +export const APPROVE_HOURS = 'APPROVE_HOURS'; export const requestHours = (_time, _trainee, _date) => (dispatch, getState) => { var docRef = firestore.collection('requests'); @@ -174,6 +247,10 @@ export const requestHours = (_time, _trainee, _date) => (dispatch, getState) => }); } +export const approveHours = () => (dispatch, getState) => { + alert("Admin :)"); +} + export const CREATE_FOURM_POST = 'CREATE_FOURM_POST'; export const SNAPSHOT_FOURM = 'SNAPSHOT_FOURM'; export const UPDATE_FOURM_POSTS = 'UPDATE_FOURM_POSTS'; @@ -197,7 +274,6 @@ export const snapshotFourms = () => (dispatch) => { query.forEach((doc) => { fourmPosts.push(doc.data()); }); - console.log(fourmPosts); dispatch(updateFourmPosts(fourmPosts)); }); } diff --git a/src/components/mao-account.js b/src/components/mao-account.js index f6f32c6..72eb8b8 100644 --- a/src/components/mao-account.js +++ b/src/components/mao-account.js @@ -16,7 +16,7 @@ import { connect } from 'pwa-helpers/connect-mixin.js'; import { store } from '../store.js'; //These are the actions needed by this element. -import { signIn, signOut, requestHours } from '../actions/firebase.js'; +import { signIn, signOut, requestHours, createAccount } from '../actions/firebase.js'; // We are lazy loading its reducer. import firebase from '../reducers/firebase.js'; @@ -43,7 +43,7 @@ class MaoAccount extends connect(store)(PageViewElement) { @@ -113,6 +117,29 @@ class MaoAccount extends connect(store)(PageViewElement) { + + `; } @@ -134,17 +161,19 @@ class MaoAccount extends connect(store)(PageViewElement) { } logIn() { - var emailElement = this.shadowRoot.getElementById('emailField'); - var passwordElement = this.shadowRoot.getElementById('passwordField'); + if(this.shadowRoot) { + var emailElement = this.shadowRoot.getElementById('emailField'); + var passwordElement = this.shadowRoot.getElementById('passwordField'); - const email = emailElement.value - + "@communityschoolnaples.org"; - const password = passwordElement.value; + const email = emailElement.value + + "@communityschoolnaples.org"; + const password = passwordElement.value; - store.dispatch(signIn(email,password)); + store.dispatch(signIn(email,password)); - emailElement.value = ""; - passwordElement.value = ""; + emailElement.value = ""; + passwordElement.value = ""; + } } createCompetitionList(comps) { @@ -166,6 +195,27 @@ class MaoAccount extends connect(store)(PageViewElement) { } } + makeAccount() { + var emailElement = this.shadowRoot.getElementById('createEmailField'); + var passwordElement = this.shadowRoot.getElementById('createPasswordField'); + + if( emailElement.value.includes('2019') || + emailElement.value.includes('2020') + ) + { + const email = emailElement.value + + "@communityschoolnaples.org"; + const password = passwordElement.value; + + store.dispatch(createAccount(email,password)); + } else { + alert('Please use a validated email.'); + } + + emailElement.value = ""; + passwordElement.value = ""; + } + } window.customElements.define('mao-account', MaoAccount); diff --git a/src/components/mao-app.js b/src/components/mao-app.js index 5a02542..55e27b6 100644 --- a/src/components/mao-app.js +++ b/src/components/mao-app.js @@ -210,7 +210,6 @@ class MaoApp extends connect(store)(LitElement) { Home Tutoring Compete - Compete Fourms Account diff --git a/src/components/mao-fourms.js b/src/components/mao-fourms.js index 78f0192..4aa9873 100644 --- a/src/components/mao-fourms.js +++ b/src/components/mao-fourms.js @@ -170,13 +170,13 @@ class MaoFourms extends connect(store)(PageViewElement) { var postsGrid = this.shadowRoot.getElementById('posts-grid'); postsGrid.innerHTML = ""; - for(var i = 0; i < this.fourmPosts.length; i++) { + for(var i = this.fourmPosts.length-1; i >=0; i--) { var paperCard = document.createElement('paper-card'); var cardContent = document.createElement('div'); var fourmAuthor = document.createElement('h4'); var fourmSubject = document.createElement('h3'); var fourmContent = document.createElement('p'); - + fourmAuthor.innerHTML = this.fourmPosts[i].email .replace('@communityschoolnaples.org', ''); fourmSubject.innerHTML = this.fourmPosts[i].subject; fourmContent.innerHTML = this.fourmPosts[i].content; diff --git a/src/components/mao-tutoring.js b/src/components/mao-tutoring.js index 3a833c6..bca76c1 100644 --- a/src/components/mao-tutoring.js +++ b/src/components/mao-tutoring.js @@ -95,8 +95,8 @@ class MaoTutoring extends connect(store)(PageViewElement) { display: block; } - .hopeCard { - --paper-card-background-color: #58d68d; + .subjectCard, .hopeCard { + --paper-card-background-color: #ffffff; } paper-item { @@ -147,12 +147,11 @@ class MaoTutoring extends connect(store)(PageViewElement) { -
+